webpack配置
增加ts-loader
{
test: /\.(ts|tsx)$/,
include,
exclude: /node_modules/,
use: ['ts-loader']
}
ESLINT配置
注意使用overrides属性,可以在overrides属性里配置对应规则
{
"root": true,
"env": {
"serviceworker": true,
"browser": true,
"es6": true,
"jest/globals": true
},
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"overrides": [
{
"files": [
"./src/**/*.js?(x)"
],
"parser": "@babel/eslint-parser",
"plugins": [
"jsx-a11y",
"react",
"react-hooks",
"prettier",
"jest"
],
"extends": [
"eslint:recommended",
"plugin:prettier/recommended",
"plugin:jest/recommended"
],
"rules": {
"no-unused-vars": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/explicit-module-boundary-types": 0,
"prettier/prettier": 1,
// 使用prettier作为eslint规则
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
// 以上两行是精华,必须添加,使用钩子函数时才会提示警告/错误
"react/jsx-filename-extension": [
1,
{
"extensions": [
".js",
".jsx"
]
}
],
"linebreak-style": 0,
"no-plusplus": [
1,
{
"allowForLoopAfterthoughts": true
}
],
"react/react-in-jsx-scope": 0,
"react/jsx-props-no-spreading": 0,
"import/prefer-default-export": 0,
"no-unused-expressions": [
"error",
{
"allowShortCircuit": true,
"allowTernary": true
}
],
"no-restricted-syntax": 0,
"no-use-before-define": [
"error",
{
"functions": false
}
],
"jsx-a11y/no-static-element-interactions": 0,
"jsx-a11y/interactive-supports-focus": [
"error",
{
"tabbable": [
"button",
"checkbox",
"link",
"searchbox",
"spinbutton",
"switch",
"textbox"
]
}
],
"react/destructuring-assignment": 0,
"jsx-a11y/click-events-have-key-events": 0,
"jest/no-disabled-tests": "warn",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error",
"class-methods-use-this": 0,
"no-param-reassign": 0,
"no-console": [
"error",
{
"allow": [
"warn",
"error"
]
}
],
"default-param-last": 0
}
},
{
"files": [
"./src/**/*.ts?(x)"
],
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"react-hooks"
],
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"rules": {
"react/forbid-prop-types": 0,
"@typescript-eslint/explicit-function-return-type": 2,
"@typescript-eslint/explicit-module-boundary-types": 2,
"prettier/prettier": 1,
// 使用prettier作为eslint规则
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
// 以上两行是精华,必须添加,使用钩子函数时才会提示警告/错误
"react/jsx-filename-extension": [
1,
{
"extensions": [
".ts",
".tsx"
]
}
],
"linebreak-style": 0,
"react/prop-types": 0,
"no-plusplus": [
1,
{
"allowForLoopAfterthoughts": true
}
],
"react/react-in-jsx-scope": 0,
"react/jsx-props-no-spreading": 0,
"import/prefer-default-export": 0,
"no-unused-expressions": [
"error",
{
"allowShortCircuit": true,
"allowTernary": true
}
],
"no-restricted-syntax": 0,
"jsx-a11y/no-static-element-interactions": 0,
"jsx-a11y/interactive-supports-focus": 0,
"react/destructuring-assignment": 0,
"jsx-a11y/click-events-have-key-events": 0,
"@typescript-eslint/no-use-before-define": "off",
"no-use-before-define": "off",
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true,
"types": {
"{}": false
}
}
],
"@typescript-eslint/no-var-requires": 0,
"react/display-name": 0
}
}
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 6,
"sourceType": "module"
}
}
tsconfig配置
"allowJs": true, "checkJs": false, "esModuleInterop": true, "allowSyntheticDefaultImports": true,
注意以上属性的配置,否则eslint检查会对js使用ts规则
{
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"jsx": "react-jsx",
"allowJs": true,
"checkJs": false,
"noImplicitAny": false
},
"include": [
"./src/**/*.(ts|tsx)",
"types"
],
"exclude": [
"node_modules"
]
}
package.json配置
{
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.6",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"ts-loader": "^9.3.1",
"typescript": "^4.7.4",
}

1316

被折叠的 条评论
为什么被折叠?



