consistent-type-definitions
Enforce type definitions to consistently use either
interface
ortype
.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/stylistic"
可启用此规则。
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
TypeScript 提供了两种定义对象类型的常用方法:interface
和 type
。
¥TypeScript provides two common ways to define an object type: interface
and type
.
// type alias
type T1 = {
a: string;
b: number;
};
// interface keyword
interface T2 {
a: string;
b: number;
}
两者通常非常相似,并且经常可以互换使用。一致地使用相同的类型声明样式有助于提高代码的可读性。
¥The two are generally very similar, and can often be used interchangeably. Using the same type declaration style consistently helps with code readability.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/consistent-type-definitions": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/consistent-type-definitions": "error"
}
};
在线运行试试这个规则 ↗
选项
该规则接受以下选项:
type Options = [
/** Which type definition syntax to prefer. */
| 'type'
/** Which type definition syntax to prefer. */
| 'interface',
];
const defaultOptions: Options = ['interface'];
¥Options
-
'interface'
(默认):强制使用interface
进行对象类型定义。¥
'interface'
(default): enforce usinginterface
s for object type definitions. -
'type'
:强制使用type
进行对象类型定义。¥
'type'
: enforce usingtype
s for object type definitions.
'interface'
- ❌ Incorrect
- ✅ Correct
type T = { x: number };
Open in Playgroundtype T = string;
type Foo = string | {};
interface T {
x: number;
}
Open in Playground'type'
- ❌ Incorrect
- ✅ Correct
interface T {
x: number;
}
Open in Playgroundtype T = { x: number };
Open in Playground常见问题
¥FAQs
interface
和 type
之间有什么区别?
¥What are the differences between interface
and type
?
TypeScript 中的接口和对象类型之间几乎没有区别。除了用于表示联合类 型的类型别名之外,你很少需要选择其中一个。
¥There are very few differences between interfaces and object types in TypeScript. Other than type aliases being used to represent union types, it is rare that you will need to choose one over the other.
功能 | 接口 | 对象类型 | 说明 |
---|---|---|---|
对象形状 | ✅ | ✅ | 两者都可用于表示一般对象形状。 |
一般性能 | ✅ | ✅ | 两者都针对 TypeScript 类型检查器的性能进行了优化。 |
边缘情况性能 | ✅ | 大型、复杂的逻辑类型可以通过 TypeScript 的类型检查器使用接口进行更好的优化。 | |
传统语义 | ✅ | 接口通常是 TypeScript 社区中大部分(但不是全部)的默认设置。 | |
非对象形状 | ✅ | 对象类型可以描述文字、原语、联合和交集。 | |
逻辑类型 | ✅ | 对象类型可以包括条件类型和映射类型。 | |
合并 | 允许 | 不允许 | 同名的接口被视为一个接口("merged");类型别名不能共享名称。 |
我们建议选择一种定义样式,尽可能使用它,并在需要时回退到另一种样式。在代码库中保持一致的好处几乎总是超过任何一种定义风格的好处。
¥We recommend choosing one definition style, using it when possible, and falling back to the other style when needed. The benefits of remaining consistent within a codebase almost always outweigh the benefits of either definition style.
interface
和 type
之间的性能差异何时重要?
¥When do the performance differences between interface
and type
matter?
几乎从不。大多数 TypeScript 项目不会 - 也不应该 - 使用能够体现两种定义之间性能差异的类型。
¥Almost never. Most TypeScript projects do not -and should not- utilize types that exercise the performance differences between the two kinds of definitions.
如果你在类型检查性能方面遇到问题,请参阅 TypeScript Wiki 的性能页面。
¥If you are having problems with type checking performance, see the TypeScript Wiki's Performance page.
为什么默认 interface
?
¥Why is the default interface
?
接口是 TypeScript 中最常见的流行样式。interface
传统上是 TypeScript 传达 "具有这些字段的对象" 的预期("semantic")方式。
¥Interfaces are the prevailing, most common style in the TypeScript.
interface
has traditionally been TypeScript's intended ("semantic") way to convey "an object with these fields".
我们通常建议保留默认值 'interface'
,以与大多数 TypeScript 项目在风格上保持一致。如果你非常喜欢 'type'
,那也没问题。
¥We generally recommend staying with the default, 'interface'
, to be stylistically consistent with the majority of TypeScript projects.
If you strongly prefer 'type'
, that's fine too.
何时不使用它
¥When Not To Use It
如果你每次定义类型时都特别想出于风格原因手动选择是使用接口还是类型文字,则可以避免此规则。
¥If you specifically want to manually choose whether to use an interface or type literal for stylistic reasons each time you define a type, you can avoid this rule.
但是,请记住,不一致的风格可能会损害项目的可读性。我们建议为此规则选择一个最适合你的项目的选项。
¥However, keep in mind that inconsistent style can harm readability in a project. We recommend picking a single option for this rule that works best for your project.
你可以考虑使用 ,而不是完全禁用此规则。考虑在那些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
¥You might occasionally need to a different definition type in specific cases, such as if your project is a dependency or dependent of another project that relies on a specific type definition style. Consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
进一步阅读
¥Further Reading
-
TypeScript 手册 > 日常类型 > 类型别名和接口
¥TypeScript Handbook > Everyday Types > Differences Between Type Aliases and Interfaces
'## 资源'