gpt4 book ai didi

typescript - eslint 未检测到 TS 错误

转载 作者:行者123 更新时间:2023-12-05 00:53:25 41 4
gpt4 key购买 nike

我有一个使用 typescript 的 nextJS 应用程序。我使用 VSCode 进行开发。我注意到当我打开一个有错误但 eslint 没有的文件时,VSCode 会按预期显示 TS 错误。

例如,这是我的 VSCode 错误:

VSCode TS error

但这是我的 linting 结果:

▶ yarn lint

yarn run v1.22.10
$ eslint .
(node:83388) ExperimentalWarning: Conditional exports is an experimental feature. This feature could change at any time
(node:83388) ExperimentalWarning: Package name self resolution is an experimental feature. This feature could change at any time
✨ Done in 5.26s.

以下是一些可能有助于识别问题的文件:

.eslintrc.js:

module.exports = {
plugins: [
'@typescript-eslint',
],
extends: [
'airbnb-typescript',
'airbnb/hooks',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
ignorePatterns: [
'app.js',
'.eslintrc.js',
'jest.config.js',
'babel.config.js',
'/plugins',
],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
},
rules: {
'react/require-default-props': ['warn', { ignoreFunctionalComponents: true }],
},
overrides: [
{
files: ['*.spec.tsx', '*.stories.tsx'],
rules: {
'import/no-extraneous-dependencies': 'off',
}
}
]
};

tsconfig.json:

{
"compilerOptions": {
"target": "ES2016",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}

bable.config.js:

const path = require('path');

module.exports = {
presets: [
[
'next/babel',
{
'styled-jsx': {
plugins: [path.join(__dirname, '/plugins/styled-jsx-plugin-sass.js')],
}
}
]
],
plugins: ['inline-react-svg']
}

package.json 脚本:

{
"scripts": {
"preinstall": "npx only-allow npm",
"dev": "next dev",
"build": "next build",
"start": "next start",
"storybook": "start-storybook -s ./public -p 6006",
"build-storybook": "build-storybook",
"lint": "eslint .",
"test": "jest"
},
}

我非常乐意提供任何进一步的信息来帮助解决问题。提前谢谢!

最佳答案

经过更多研究,我发现这个 github 问题具有相同的问题:https://github.com/typescript-eslint/typescript-eslint/issues/352

答案是:

we purposely do not care about errors that should arise as a result of type checking errors.We only care that the code is syntactically valid (i.e. can be parsed without errors), not about whether it's semantically valid (i.e. don't care if it is type sound)

If you want your code to be typechecked - please use tsc (or one of the tools built for your specific build chain).

所以我的解决方案是将我的 lint 脚本编辑为:

{
"scripts": {
"lint": "tsc --noEmit && eslint .",
},
}

--noEmit 标志告诉命令不要生成任何文件。

关于typescript - eslint 未检测到 TS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67883732/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com