类型化检查
编辑器 ESLint 报告已过时文 件更改后
¥Editor ESLint reports become out-of-date after file changes
至少在 VS Code 中,类型化 linting 存在一个已知问题,即在多次更新磁盘上的文件后,文件可能会出现过时的 lint 错误。根本原因是 ESLint 没有为编辑器提供了解跨文件依赖(例如类型信息)的方法。这会导致文件在导入的文件发生更改时接收过时的类型信息。
¥There is a known issue in at least VS Code with typed linting that files might have out-of-date lint errors after multiple updates to files on disk. The root cause is that ESLint doesn't provide a way for editors to know about cross-file dependencies, such as type information. This results in files receiving out-of-date type information when files they import from are changed.
你可能会将此视为对 any 或 error typed 值的错误 no-unsafe-* 规则投诉。其他规则可能更微妙地不正确。
¥You might see this as erroneous no-unsafe-* rule complaints on any or error typed values.
Other rules may be more subtly incorrect.
目前,解决方法是在类型过期时在 VS Code 中运行 Restart ESLint Server 命令(或其他编辑器中的等效命令)。
¥For now, the workaround is to run the Restart ESLint Server command in VS Code (or an equivalent in other editors) when types get out of date.
有关更多信息,请参阅 ESLint 不重新计算文件更改的跨文件信息 (microsoft/vscode-eslint#1774)。
¥See ESLint does not re-compute cross-file information on file changes (microsoft/vscode-eslint#1774) for more information.
如何禁用文件的类型检查 linting?
¥How do I disable type-checked linting for a file?
使用 ESLint 的配置对象 和我们的 disable-type-checked 配置来禁用包含该文件的 files 匹配的类型检查。
¥Use ESLint's configuration objects with our disable-type-checked config to disable type checking for a files match that includes that file.
例如,要禁用所有 .js 文件上的类型检查 linting:
¥For example, to disable type-checked linting on all .js files:
- 扁平配置
- 旧版配置
import defineConfig from 'eslint/config';
import tseslint from 'typescript-eslint';
export default defineConfig(
// ... the rest of your config ...
{
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
},
);
module.exports = {
// ... the rest of your config ...
overrides: [
{
extends: ['plugin:@typescript-eslint/disable-type-checked'],
files: ['./**/*.js'],
},
],
};
或者,要手动禁用文件的类型检查,你可以将 parserOptions: { project: false } 设置为要排除的文件的覆盖。
¥Alternatively to disable type checking for files manually, you can set parserOptions: { project: false } to an override for the files you wish to exclude.
如何禁用一组文件的类型感知 linting?
¥How can I disable type-aware linting for a set of files?
你可以将 ESLint 的 overrides 配置与我们的 disable-type-checked 配置结合起来,以关闭特定文件子集上的类型感知 linting。
¥You can combine ESLint's overrides config in conjunction with our disable-type-checked config to turn off type-aware linting on specific subsets of files.
- 扁平配置
- 旧版配置
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommendedTypeChecked,
tseslint.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
},
},
},
{
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
},
);
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
],
plugins: ['@typescript-eslint'],
parser: '@typescript-eslint/parser',
parserOptions: {
projectService: true,
tsconfigRootDir: __dirname,
},
root: true,
overrides: [
{
files: ['*.js'],
extends: ['plugin:@typescript-eslint/disable-type-checked'],
},
],
};
如果你使用其他插件的类型感知规则,则需要手动禁用这些规则或使用它们提供的预制配置来禁用它们。
¥If you use type-aware rules from other plugins, you will need to manually disable these rules or use a premade config they provide to disable them.