跳至主要内容

禁止类型别名

禁止类型别名。

已弃用

此规则已被弃用,建议使用 @typescript-eslint/consistent-type-definitions 规则。TypeScript 类型别名是常用的语言特性,完全禁止它通常会适得其反。

注意

如果您想禁止某些类型的类型别名,请考虑使用 no-restricted-syntax。请参阅 故障排除和常见问题解答

在 TypeScript 中,类型别名有三个用途

  • 为其他类型创建别名,以便我们可以使用更简单的名称来引用它们。
// this...
type Person = {
firstName: string;
lastName: string;
age: number;
};

function addPerson(person: Person) {
// ...
}

// is easier to read than this...
function addPerson(person: {
firstName: string;
lastName: string;
age: number;
}) {
// ...
}
  • 类似于接口,提供实现该类型的对象必须具有的方法和属性集。
type Person = {
firstName: string;
lastName: string;
age: number;
walk: () => void;
talk: () => void;
};

// you know person will have 3 properties and 2 methods,
// because the structure has already been defined.
var person: Person = {
// ...
};

// so we can be sure that this will work
person.walk();
  • 充当类型之间的映射工具,以允许快速修改。
type Immutable<T> = { readonly [P in keyof T]: T[P] };

type Person = {
name: string;
age: number;
};

type ImmutablePerson = Immutable<Person>;

var person: ImmutablePerson = { name: 'John', age: 30 };
person.name = 'Brad'; // error, readonly property

在创建别名时,类型别名不会创建新的类型,它只是创建了一个新的名称来引用原始类型。因此,为基本类型和其他简单类型、元组、联合或交集创建别名有时可能是多余的。

// this doesn't make much sense
type myString = string;

另一方面,使用类型别名作为接口会限制您

  • 代码重用:接口可以被其他类型扩展或实现。类型别名不能。
  • 调试代码:接口创建了一个新的名称,因此在调试应用程序时很容易识别对象的基类型。

最后,映射类型是一种高级技术,如果将其开放,可能会很快成为应用程序中的一个痛点。

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-type-alias": "error"
}
};

在游乐场中尝试此规则 ↗

示例

此规则禁止使用类型别名,而建议使用接口和简化的类型(基本类型、元组、联合、交集等)。

选项

此规则接受以下选项

type ExpandedOptions =
| 'always'
| 'in-intersections'
| 'in-unions'
| 'in-unions-and-intersections'
| 'never';

type SimpleOptions = 'always' | 'never';

type Options = [
{
/** Whether to allow direct one-to-one type aliases. */
allowAliases?: ExpandedOptions;
/** Whether to allow type aliases for callbacks. */
allowCallbacks?: SimpleOptions;
/** Whether to allow type aliases for conditional types. */
allowConditionalTypes?: SimpleOptions;
/** Whether to allow type aliases with constructors. */
allowConstructors?: SimpleOptions;
/** Whether to allow type aliases with generic types. */
allowGenerics?: SimpleOptions;
/** Whether to allow type aliases with object literal types. */
allowLiterals?: ExpandedOptions;
/** Whether to allow type aliases with mapped types. */
allowMappedTypes?: ExpandedOptions;
/** Whether to allow type aliases with tuple types. */
allowTupleTypes?: ExpandedOptions;
},
];

const defaultOptions: Options = [
{
allowAliases: 'never',
allowCallbacks: 'never',
allowConditionalTypes: 'never',
allowConstructors: 'never',
allowLiterals: 'never',
allowMappedTypes: 'never',
allowTupleTypes: 'never',
allowGenerics: 'never',
},
];

allowAliases

这适用于基本类型和引用类型。

该设置接受以下值

  • "always""never" 用于激活或停用该功能。
  • "in-unions",允许在联合语句中使用别名,例如 type Foo = string | string[];
  • "in-intersections",允许在交集语句中使用别名,例如 type Foo = string & string[];
  • "in-unions-and-intersections",允许在联合和/或交集语句中使用别名。

{ "allowAliases": "always" } 选项的**正确**代码示例

// primitives
type Foo = 'a';

type Foo = 'a' | 'b';

type Foo = string;

type Foo = string | string[];

type Foo = string & string[];

type Foo = `foo-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}

type Foo = Bar;

type Foo = Bar | Baz;

type Foo = Bar & Baz;
在 Playground 中打开

{ "allowAliases": "in-unions" } 选项的**错误**代码示例

// primitives
type Foo = 'a';

type Foo = string;

type Foo = string & string[];

type Foo = `foo-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}

type Foo = Bar;

type Foo = Bar & Baz;
在 Playground 中打开

{ "allowAliases": "in-unions" } 选项的**正确**代码示例

// primitives
type Foo = 'a' | 'b';

type Foo = string | string[];

type Foo = `a-${number}` | `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}

type Foo = Bar | Baz;
在 Playground 中打开

{ "allowAliases": "in-intersections" } 选项的**错误**代码示例

// primitives
type Foo = 'a';

type Foo = 'a' | 'b';

type Foo = string;

type Foo = string | string[];

type Foo = `a-${number}` | `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}

type Foo = Bar;

type Foo = Bar | Baz;
在 Playground 中打开

{ "allowAliases": "in-intersections" } 选项的**正确**代码示例

// primitives
type Foo = string & string[];

type Foo = `a-${number}` & `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}

type Foo = Bar & Baz;
在 Playground 中打开

{ "allowAliases": "in-unions-and-intersections" } 选项的**错误**代码示例

// primitives
type Foo = 'a';

type Foo = string;

type Foo = `foo-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}

type Foo = Bar;
在 Playground 中打开

{ "allowAliases": "in-unions-and-intersections" } 选项的**正确**代码示例

// primitives
type Foo = 'a' | 'b';

type Foo = string | string[];

type Foo = string & string[];

type Foo = `a-${number}` & `b-${number}`;

type Foo = `a-${number}` | `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}

type Foo = Bar | Baz;

type Foo = Bar & Baz;
在 Playground 中打开

allowCallbacks

这适用于函数类型。

该设置接受以下值

  • "always""never" 用于激活或停用该功能。

{ "allowCallbacks": "always" } 选项的**正确**代码示例

type Foo = () => void;

type Foo = (name: string) => string;

class Person {}

type Foo = (name: string, age: number) => string | Person;

type Foo = (name: string, age: number) => string & Person;
在 Playground 中打开

allowConditionalTypes

这适用于条件类型。

{ "allowConditionalTypes": "always" } 选项的**正确**代码示例

type Foo<T> = T extends number ? number : null;
在 Playground 中打开

allowConstructors

这适用于构造函数类型。

该设置接受以下值

  • "always""never" 用于激活或停用该功能。

{ "allowConstructors": "always" } 选项的**正确**代码示例

type Foo = new () => void;
在 Playground 中打开

allowLiterals

这适用于字面量类型(type Foo = { ... })。

该设置接受以下选项

  • "always""never" 用于激活或停用该功能。
  • "in-unions",允许字面量在联合语句中,例如 type Foo = string | string[];
  • "in-intersections",允许字面量在交集语句中,例如 type Foo = string & string[];
  • "in-unions-and-intersections",允许字面量在联合和/或交集语句中。

{ "allowLiterals": "always" } 选项的正确代码示例

type Foo = {};

type Foo = {
name: string;
age: number;
};

type Foo = {
name: string;
age: number;
walk: (miles: number) => void;
};

type Foo = { name: string } | { age: number };

type Foo = { name: string } & { age: number };
在 Playground 中打开

{ "allowLiterals": "in-unions" } 选项的错误代码示例

type Foo = {};

type Foo = {
name: string;
age: number;
};

type Foo = {
name: string;
age: number;
walk: (miles: number) => void;
};

type Foo = { name: string } & { age: number };
在 Playground 中打开

{ "allowLiterals": "in-unions" } 选项的正确代码示例

type Foo = { name: string } | { age: number };
在 Playground 中打开

{ "allowLiterals": "in-intersections" } 选项的错误代码示例

type Foo = {};

type Foo = {
name: string;
age: number;
};

type Foo = {
name: string;
age: number;
walk: (miles: number) => void;
};

type Foo = { name: string } | { age: number };
在 Playground 中打开

{ "allowLiterals": "in-intersections" } 选项的正确代码示例

type Foo = { name: string } & { age: number };
在 Playground 中打开

{ "allowLiterals": "in-unions-and-intersections" } 选项的错误代码示例

type Foo = {};

type Foo = {
name: string;
age: number;
};

type Foo = {
name: string;
age: number;
walk: (miles: number) => void;
};
在 Playground 中打开

{ "allowLiterals": "in-unions-and-intersections" } 选项的正确代码示例

type Foo = { name: string } | { age: number };

type Foo = { name: string } & { age: number };
在 Playground 中打开

allowMappedTypes

这适用于字面量类型。

该设置接受以下值

  • "always""never" 用于激活或停用该功能。
  • "in-unions",允许在联合语句中使用别名,例如 type Foo = string | string[];
  • "in-intersections",允许在交集语句中使用别名,例如 type Foo = string & string[];
  • "in-unions-and-intersections",允许在联合和/或交集语句中使用别名。

{ "allowMappedTypes": "always" } 选项的正确代码示例

type Foo<T> = { readonly [P in keyof T]: T[P] };

type Foo<T> = { [P in keyof T]?: T[P] };

type Foo<T, U> =
| { readonly [P in keyof T]: T[P] }
| { readonly [P in keyof U]: U[P] };

type Foo<T, U> = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] };

type Foo<T, U> = { readonly [P in keyof T]: T[P] } & {
readonly [P in keyof U]: U[P];
};

type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
在 Playground 中打开

{ "allowMappedTypes": "in-unions" } 选项的错误代码示例

type Foo<T> = { readonly [P in keyof T]: T[P] };

type Foo<T> = { [P in keyof T]?: T[P] };

type Foo<T, U> = { readonly [P in keyof T]: T[P] } & {
readonly [P in keyof U]: U[P];
};

type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
在 Playground 中打开

{ "allowMappedTypes": "in-unions" } 选项的正确代码示例

type Foo<T, U> =
| { readonly [P in keyof T]: T[P] }
| { readonly [P in keyof U]: U[P] };

type Foo<T, U> = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] };
在 Playground 中打开

{ "allowMappedTypes": "in-intersections" } 选项的错误代码示例

type Foo<T> = { readonly [P in keyof T]: T[P] };

type Foo<T> = { [P in keyof T]?: T[P] };

type Foo<T, U> =
| { readonly [P in keyof T]: T[P] }
| { readonly [P in keyof U]: U[P] };

type Foo<T, U> = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] };
在 Playground 中打开

{ "allowMappedTypes": "in-intersections" } 选项的正确代码示例

type Foo<T, U> = { readonly [P in keyof T]: T[P] } & {
readonly [P in keyof U]: U[P];
};

type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
在 Playground 中打开

{ "allowMappedTypes": "in-unions-and-intersections" } 选项的错误代码示例

type Foo<T> = { readonly [P in keyof T]: T[P] };

type Foo<T> = { [P in keyof T]?: T[P] };
在 Playground 中打开

{ "allowMappedTypes": "in-unions-and-intersections" } 选项的正确代码示例

type Foo<T, U> =
| { readonly [P in keyof T]: T[P] }
| { readonly [P in keyof U]: U[P] };

type Foo<T, U> = { [P in keyof T]?: T[P] } | { [P in keyof U]?: U[P] };

type Foo<T, U> = { readonly [P in keyof T]: T[P] } & {
readonly [P in keyof U]: U[P];
};

type Foo<T, U> = { [P in keyof T]?: T[P] } & { [P in keyof U]?: U[P] };
在 Playground 中打开

allowTupleTypes

这适用于元组类型(type Foo = [number])。

该设置接受以下选项

  • "always""never" 用于激活或停用该功能。
  • "in-unions",允许元组在联合语句中,例如 type Foo = [string] | [string, string];
  • "in-intersections" 选项允许在交集语句中使用元组,例如 type Foo = [string] & [string, string];
  • "in-unions-and-intersections" 选项允许在联合和/或交集语句中使用元组。

以下是 { "allowTupleTypes": "always" } 选项下 **正确** 代码的示例

type Foo = [number];

type Foo = [number] | [number, number];

type Foo = [number] & [number, number];

type Foo = [number] | ([number, number] & [string, string]);
在 Playground 中打开

以下是 { "allowTupleTypes": "in-unions" } 选项下 **错误** 代码的示例

type Foo = [number];

type Foo = [number] & [number, number];

type Foo = [string] & [number];
在 Playground 中打开

以下是 { "allowTupleTypes": "in-unions" } 选项下 **正确** 代码的示例

type Foo = [number] | [number, number];

type Foo = [string] | [number];
在 Playground 中打开

以下是 { "allowTupleTypes": "in-intersections" } 选项下 **错误** 代码的示例

type Foo = [number];

type Foo = [number] | [number, number];

type Foo = [string] | [number];
在 Playground 中打开

以下是 { "allowTupleTypes": "in-intersections" } 选项下 **正确** 代码的示例

type Foo = [number] & [number, number];

type Foo = [string] & [number];
在 Playground 中打开

以下是 { "allowTupleTypes": "in-unions-and-intersections" } 选项下 **错误** 代码的示例

type Foo = [number];

type Foo = [string];
在 Playground 中打开

以下是 { "allowTupleTypes": "in-unions-and-intersections" } 选项下 **正确** 代码的示例

type Foo = [number] & [number, number];

type Foo = [string] | [number];
在 Playground 中打开

allowGenerics

此选项适用于泛型类型,包括 TypeScript 提供的全局实用类型(type Foo = Record<string, number>)。

该设置接受以下选项

  • "always""never" 用于激活或停用该功能。

以下是 { "allowGenerics": "always" } 选项下 **正确** 代码的示例

type Foo = Bar<string>;

type Foo = Record<string, number>;

type Foo = Readonly<Bar>;

type Foo = Partial<Bar>;

type Foo = Omit<Bar, 'a' | 'b'>;
在 Playground 中打开

进一步阅读

资源