consistent-type-imports
强制一致使用类型导入.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
TypeScript 允许在导入上指定 type
关键字,以指示导出仅存在于类型系统中,而不存在于运行时。这允许转译器在不知道依赖类型的情况下删除导入。
¥TypeScript allows specifying a type
keyword on imports to indicate that the export exists only in the type system, not at runtime.
This allows transpilers to drop imports without knowing the types of the dependencies.
请参阅 博客 > 一致类型导出和导入:原因和方法 以了解更多详细信息。
¥See Blog > Consistent Type Exports and Imports: Why and How for more details.
- 扁平配置
- 旧版配置
export default tseslint.config({
rules: {
"@typescript-eslint/consistent-type-imports": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/consistent-type-imports": "error"
}
};
在线运行试试这个规则 ↗
选项
该规则接受以下选项:
type Options = [
{
/** 是否禁止在类型注释(`import()`)中导入类型。 */
disallowTypeAnnotations?: boolean;
/** 当检测到导入仅在类型位置使用时,要添加的预期类型修饰符。 */
fixStyle?:
| 'inline-type-imports'
/** 当检测到导入仅在类型位置使用时,要添加的预期类型修饰符。 */
| 'separate-type-imports';
/** 仅类型导入的预期导入类型。 */
prefer?:
| 'no-type-imports'
/** 仅类型导入的预期导入类型。 */
| 'type-imports';
},
];
const defaultOptions: Options = [
{
disallowTypeAnnotations: true,
fixStyle: 'separate-type-imports',
prefer: 'type-imports',
},
];
¥Options
prefer
仅类型导入的预期导入类型。 Default: "type-imports"
.
prefer
的有效值为:
¥Valid values for prefer
are:
-
type-imports
将强制你始终使用import type Foo from '...'
,除非装饰器的元数据引用。这是默认值。¥
type-imports
will enforce that you always useimport type Foo from '...'
except referenced by metadata of decorators. It is the default. -
no-type-imports
将强制你始终使用import Foo from '...'
。¥
no-type-imports
will enforce that you always useimport Foo from '...'
.
带有 {prefer: 'type-imports'}
的正确代码示例,以及带有 {prefer: 'no-type-imports'}
的错误代码示例。
¥Examples of correct code with {prefer: 'type-imports'}
, and incorrect code with {prefer: 'no-type-imports'}
.
import type { Foo } from 'Foo';
import type Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
Open in Playground带有 {prefer: 'type-imports'}
的错误代码示例,以及带有 {prefer: 'no-type-imports'}
的正确代码。
¥Examples of incorrect code with {prefer: 'type-imports'}
, and correct code with {prefer: 'no-type-imports'}
.
import { Foo } from 'Foo';
import Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
Open in PlaygroundfixStyle
当检测到导入仅在类型位置使用时,要添加的预期类型修饰符。 Default: "separate-type-imports"
.
fixStyle
的有效值为:
¥Valid values for fixStyle
are:
-
separate-type-imports
将在 import 关键字import type { A } from '...'
后添加 type 关键字。这是默认值。¥
separate-type-imports
will add the type keyword after the import keywordimport type { A } from '...'
. It is the default. -
inline-type-imports
将内联类型关键字import { type A } from '...'
,并且仅在 TypeScript 4.5 及更高版本中可用。参见 文档在这里。¥
inline-type-imports
will inline the type keywordimport { type A } from '...'
and is only available in TypeScript 4.5 and onwards. See documentation here.
- ❌ 错误
- ✅ With `separate-type-imports`
- ✅ With `inline-type-imports`
import { Foo } from 'Foo';
import Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
Open in Playgroundimport type { Foo } from 'Foo';
import type Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
Open in Playgroundimport { type Foo } from 'Foo';
import type Bar from 'Bar';
type T = Foo;
const x: Bar = 1;
Open in PlaygrounddisallowTypeAnnotations
是否禁止在类型注释(import()