Skip to main content

TypeOrValueSpecifier

一些 lint 规则包括描述特定类型和/或值的选项。这些选项使用从 type-utilsTypeOrValueSpecifier 导出的标准化格式。

¥Some lint rules include options to describe specific types and/or values. These options use a standardized format exported from the type-utils package, TypeOrValueSpecifier.

TypeOrValueSpecifier 允许三种对象形式的说明符:

¥TypeOrValueSpecifier allows three object forms of specifiers:

例如,以下 @typescript-eslint/no-floating-promises > allowForKnownSafeCalls 配置使用包说明符将 node:testit 标记为安全:

¥For example, the following configuration of @typescript-eslint/no-floating-promises > allowForKnownSafeCalls marks node:test's it as safe using a package specifier:

{
"@typescript-eslint/no-floating-promises": [
"error",
{
"allowForKnownSafeCalls": [
{ "from": "package", "name": "it", "package": "node:test" }
]
}
]
}

每个对象格式至少需要:

¥Each object format requires at least:

  • from:使用哪个说明符,如 'file' | 'lib' | 'package'

    ¥from: which specifier to use, as 'file' | 'lib' | 'package'

  • name:要匹配的类型或值名称的 stringstring[]

    ¥name: a string or string[] for type or value name(s) to match on

FileSpecifier

interface FileSpecifier {
from: 'file';
name: string[] | string;
path?: string;
}

描述在本地文件中声明的特定类型或值。

¥Describes specific types or values declared in local files.

path 可用于指定必须在其中声明类型或值的文件。如果省略,所有文件都将匹配。

¥path may be used to specify a file the types or values must be declared in. If omitted, all files will be matched.

FileSpecifier 示例

¥FileSpecifier Examples

匹配名为 Props 的所有类型和值:

¥Matching all types and values named Props:

{ "from": "file", "name": "Props" }

匹配 file.tsx 中名为 Props 的所有类型和值:

¥Matching all types and values named Props in file.tsx:

{ "from": "file", "name": "Props", "path": "file.tsx" }

LibSpecifier

interface LibSpecifier {
from: 'lib';
name: string[] | string;
}

描述在 TypeScript 的内置 lib.*.d.ts ("lib") 类型中声明的特定类型或值。

¥Describes specific types or values declared in TypeScript's built-in lib.*.d.ts ("lib") types.

Lib 类型包括 lib.dom.d.ts 全局变量(例如 Window)和 lib.es*.ts 全局变量(例如 Array)。

¥Lib types include lib.dom.d.ts globals such as Window and lib.es*.ts globals such as Array.

LibSpecifier 示例

¥LibSpecifier Examples

匹配所有数组类型的值:

¥Matching all array-typed values:

{ "from": "lib", "name": "Array" }

匹配所有 PromisePromiseLike 类型的值:

¥Matching all Promise and PromiseLike-typed values:

{ "from": "lib", "name": ["Promise", "PromiseLike"] }

PackageSpecifier

interface PackageSpecifier {
from: 'package';
name: string[] | string;
package: string;
}

描述从包中导入的特定类型或值。

¥Describes specific types or values imported from packages.

必须使用 package 来指定包名称。

¥package must be used to specify the package name.

PackageSpecifier 示例

¥PackageSpecifier Examples

匹配来自 @reduxjs/toolkitSafePromise 类型:

¥Matching the SafePromise type from @reduxjs/toolkit:

{ "from": "package", "name": "SafePromise", "package": "@reduxjs/toolkit" }

匹配来自 vitestdescribeittest 值:

¥Matching the describe, it, and test values from vitest:

{ "from": "package", "name": ["describe", "it", "test"], "package": "vitest" }

通用字符串说明符

¥Universal String Specifiers

TypeOrValueSpecifier 还允许提供普通字符串说明符来匹配所有名称,而不管声明源如何。例如,提供 "RegExp" 匹配名为 RegExp 的所有类型和值。

¥TypeOrValueSpecifier also allows providing a plain string specifier to match all names regardless of declaration source. For example, providing "RegExp" matches all types and values named RegExp.

危险

我们强烈建议不要使用通用字符串说明符。匹配所有名称而不指定源文件、库或包可能会意外匹配具有巧合相似名称的其他类型或值。

¥We strongly recommend not using universal string specifiers. Matching all names without specifying a source file, library, or package can accidentally match other types or values with a coincidentally similar name.

通用字符串说明符将在 typescript-eslint 的未来主要版本中删除。

¥Universal string specifiers will be removed in a future major version of typescript-eslint.

使用此格式的规则选项

¥Rule Options Using This Format