错误示例:
// ❌ 不要这样写
import { CredentialManager } from './credential-manager/CredentialManager.js'
import { Platform } from '../types/credential.ts'
import utils from './utils/helper.js'
正确示例:
// ✅ 正确写法
import { CredentialManager } from './credential-manager/CredentialManager'
import { Platform } from '../types/credential'
import utils from './utils/helper'
// ✅ 推荐:使用配置的路径别名
import { CredentialManager } from '@/core/credential-manager'
import { Platform } from '@/types/credential'
import { logger } from '@/utils/logger'
// 1. Node.js 内置模块
import { readFile } from 'fs'
import { join } from 'path'
// 2. 第三方 npm 包
import { describe, test, expect } from '@jest/globals'
import axios from 'axios'
// 3. 内部模块(@/ 开头)
import { CredentialManager } from '@/core/credential-manager'
import { Platform } from '@/types/credential'
// 4. 相对路径导入
import { helper } from '../utils/helper'
import { config } from './config'
项目已配置 ESLint 规则自动检查 import 语句:
import/extensions
: 禁止使用文件扩展名import/no-unresolved
: 确保导入路径存在import/no-duplicates
: 禁止重复导入import/order
: 自动排序导入语句# 检查所有文件
npm run lint
# 自动修复可修复的问题
npm run lint:fix
# 检查特定文件
npx eslint src/path/to/file.ts
在 .vscode/settings.json
中添加:
{
"typescript.suggest.includeCompletionsForModuleExports": true,
"typescript.suggest.includeCompletionsWithInsertText": true,
"typescript.preferences.includePackageJsonAutoImports": "auto",
"typescript.suggest.autoImports": true,
// 自动移除文件扩展名
"typescript.suggest.paths": false,
// ESLint 自动修复
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.organizeImports": true
},
// 保存时自动格式化
"editor.formatOnSave": true
}
false
Never
A:
A:
tsconfig.json
中的 paths
配置正确npm install
确保依赖安装完整A:
npm run lint:fix
自动修复# 在 .husky/pre-commit 中添加
npm run lint
记住:一致的 import 风格让代码更易读、易维护!🚀