feat: 接入Ts配置

This commit is contained in:
zdl
2025-11-14 16:15:29 +08:00
parent e23feb3c23
commit 48fdca203c
10 changed files with 663 additions and 31 deletions

92
.eslintrc.js Normal file
View File

@@ -0,0 +1,92 @@
module.exports = {
root: true,
/* 环境配置 */
env: {
browser: true,
es2021: true,
node: true,
},
/* 扩展配置 */
extends: [
'react-app', // Create React App 默认规则
'react-app/jest', // Jest 测试规则
'eslint:recommended', // ESLint 推荐规则
'plugin:react/recommended', // React 推荐规则
'plugin:react-hooks/recommended', // React Hooks 规则
'plugin:prettier/recommended', // Prettier 集成
],
/* 解析器选项 */
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
/* 插件 */
plugins: ['react', 'react-hooks', 'prettier'],
/* 规则配置 */
rules: {
// React
'react/react-in-jsx-scope': 'off', // React 17+ 不需要导入 React
'react/prop-types': 'off', // 使用 TypeScript 类型检查,不需要 PropTypes
'react/display-name': 'off', // 允许匿名组件
// 通用
'no-console': ['warn', { allow: ['warn', 'error'] }], // 仅警告 console.log
'no-unused-vars': ['warn', {
argsIgnorePattern: '^_', // 忽略以 _ 开头的未使用参数
varsIgnorePattern: '^_', // 忽略以 _ 开头的未使用变量
}],
'prettier/prettier': ['warn', {}, { usePrettierrc: true }], // 使用项目的 Prettier 配置
},
/* 设置 */
settings: {
react: {
version: 'detect', // 自动检测 React 版本
},
},
/* TypeScript 文件特殊配置 */
overrides: [
{
files: ['**/*.ts', '**/*.tsx'], // 仅对 TS 文件应用以下配置
parser: '@typescript-eslint/parser', // 使用 TypeScript 解析器
parserOptions: {
project: './tsconfig.json', // 关联 tsconfig.json
},
extends: [
'plugin:@typescript-eslint/recommended', // TypeScript 推荐规则
],
plugins: ['@typescript-eslint'],
rules: {
// TypeScript 特定规则
'@typescript-eslint/no-explicit-any': 'warn', // 警告使用 any允许但提示
'@typescript-eslint/explicit-module-boundary-types': 'off', // 不强制导出函数类型
'@typescript-eslint/no-unused-vars': ['warn', {
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
}],
'@typescript-eslint/no-non-null-assertion': 'warn', // 警告使用 !(非空断言)
// 覆盖基础规则(避免与 TS 规则冲突)
'no-unused-vars': 'off', // 使用 TS 版本的规则
},
},
],
/* 忽略文件(与 .eslintignore 等效)*/
ignorePatterns: [
'node_modules/',
'build/',
'dist/',
'*.config.js',
'public/mockServiceWorker.js',
],
};