我正在尝试使用打字稿以及纯JavaScript。因此,有些文件是 javascript 文件 (.js),而其他文件是 typescript 文件 (.ts)。这有效。
但是,我在 VSCode 中的 eslint 给了我 Typescript 错误和警告,即使是在纯 JavaScript 文件中也是如此。
我可以为 .js 文件关闭这些功能,但只要文件是
.ts
/ .tsx
时就保持打开状态吗?
我的 eslint 和 typescript 配置只是 Vercels 官方 typescript 示例。
.eslintignore
:
**/node_modules/*
**/out/*
**/.next/*
.eslintrc.json
:
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
// Uncomment the following lines to enable eslint-config-prettier
// Is not enabled right now to avoid issues with the Next.js repo
"prettier",
"prettier/@typescript-eslint"
],
"env": {
"es6": true,
"browser": true,
"jest": true,
"node": true
},
"settings": {
"react": {
"version": "detect"
}
},
"rules": {
"react/react-in-jsx-scope": 0,
"react/display-name": 0,
"react/prop-types": 0,
"@typescript-eslint/explicit-function-return-type": 0,
"@typescript-eslint/explicit-member-accessibility": 0,
"@typescript-eslint/indent": 0,
"@typescript-eslint/member-delimiter-style": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-use-before-define": 0,
"@typescript-eslint/no-unused-vars": [
2,
{
"argsIgnorePattern": "^_"
}
],
"no-console": [
2,
{
"allow": ["warn", "error"]
}
]
}
}
tsconfig.json
:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": ["node_modules", ".next", "out", "**/*.js", "**/*.jsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}
我想我也有同样的问题。这花了我一段时间才弄清楚,但我想我已经明白了。 要禁用 JavaScript 文件的类型检查,请将
checkJs: false
添加到项目根目录中 compilerOptions
的 jsconfig.json
部分。
这是我的
jsconfig.json
:
{
"include": ["./src/**/*"],
"compilerOptions": {
"checkJs": false
}
}
在您的
.eslintignore
文件中添加此行
**/*.js
eslint 不会检查任何 *.js 文件。