no-type-alias
Disallow type aliases.
This rule has been deprecated in favour of the @typescript-eslint/consistent-type-definitions
rule.
TypeScript type aliases are a commonly necessary language feature; banning it altogether is oftentimes counterproductive.
If you want to ban certain classifications of type aliases, consider using no-restricted-syntax
.
See Troubleshooting & FAQs.
In TypeScript, type aliases serve three purposes:
- Aliasing other types so that we can refer to them using a simpler name.
// 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;
}) {
// ...
}
- Act sort of like an interface, providing a set of methods and properties that must exist in the objects implementing the type.
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();
- Act like mapping tools between types to allow quick modifications.
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
When aliasing, the type alias does not create a new type, it just creates a new name to refer to the original type. So aliasing primitives and other simple types, tuples, unions or intersections can some times be redundant.
// this doesn't make much sense
type myString = string;
On the other hand, using a type alias as an interface can limit your ability to:
- Reuse your code: interfaces can be extended or implemented by other types. Type aliases cannot.
- Debug your code: interfaces create a new name, so is easy to identify the base type of an object while debugging the application.
Finally, mapping types is an advanced technique and leaving it open can quickly become a pain point in your application.
module.exports = {
"rules": {
"@typescript-eslint/no-type-alias": "error"
}
};
Try this rule in the playground ↗
Examples
This rule disallows the use of type aliases in favor of interfaces and simplified types (primitives, tuples, unions, intersections, etc).
Options
This rule accepts the following options:
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
This applies to primitive types and reference types.
The setting accepts the following values:
"always"
or"never"
to active or deactivate the feature."in-unions"
, allows aliasing in union statements, e.g.type Foo = string | string[];
"in-intersections"
, allows aliasing in intersection statements, e.g.type Foo = string & string[];
"in-unions-and-intersections"
, allows aliasing in union and/or intersection statements.
Examples of correct code for the { "allowAliases": "always" }
options:
// 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;
Open in PlaygroundExamples of incorrect code for the { "allowAliases": "in-unions" }
option:
// 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;
Open in PlaygroundExamples of correct code for the { "allowAliases": "in-unions" }
option:
// 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;
Open in PlaygroundExamples of incorrect code for the { "allowAliases": "in-intersections" }
option:
// 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;
Open in PlaygroundExamples of correct code for the { "allowAliases": "in-intersections" }
option:
// primitives
type Foo = string & string[];
type Foo = `a-${number}` & `b-${number}`;
// reference types
interface Bar {}
class Baz implements Bar {}
type Foo = Bar & Baz;
Open in PlaygroundExamples of incorrect code for the { "allowAliases": "in-unions-and-intersections" }
option:
// primitives
type Foo = 'a';
type Foo = string;
type Foo = `foo-${number}`;
// reference types
interface Bar {}
class Baz implements Bar {}
type Foo = Bar;
Open in PlaygroundExamples of correct code for the { "allowAliases": "in-unions-and-intersections" }
option:
// 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;
Open in PlaygroundallowCallbacks
This applies to function types.
The setting accepts the following values:
"always"
or"never"
to active or deactivate the feature.
Examples of correct code for the { "allowCallbacks": "always" }
option:
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;
Open in PlaygroundallowConditionalTypes
This applies to conditional types.
Examples of correct code for the { "allowConditionalTypes": "always" }
option:
type Foo<T> = T extends number ? number : null;
Open in PlaygroundallowConstructors
This applies to constructor types.
The setting accepts the following values:
"always"
or"never"
to active or deactivate the feature.
Examples of correct code for the { "allowConstructors": "always" }
option:
type Foo = new () => void;
Open in PlaygroundallowLiterals
This applies to literal types (type Foo = { ... }
).
The setting accepts the following options:
"always"
or"never"
to active or deactivate the feature."in-unions"
, allows literals in union statements, e.g.type Foo = string | string[];
"in-intersections"
, allows literals in intersection statements, e.g.type Foo = string & string[];
"in-unions-and-intersections"
, allows literals in union and/or intersection statements.
Examples of correct code for the { "allowLiterals": "always" }
options:
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 };
Open in PlaygroundExamples of incorrect code for the { "allowLiterals": "in-unions" }
option:
type Foo = {};
type Foo = {
name: string;
age: number;
};
type Foo = {
name: string;
age: number;
walk: (miles: number) => void;
};
type Foo = { name: string } & { age: number };
Open in PlaygroundExamples of correct code for the { "allowLiterals": "in-unions" }
option:
type Foo = { name: string } | { age: number };
Open in PlaygroundExamples of incorrect code for the { "allowLiterals": "in-intersections" }
option:
type Foo = {};
type Foo = {
name: string;
age: number;
};
type Foo = {
name: string;
age: number;
walk: (miles: number) => void;
};
type Foo = { name: string } | { age: number };
Open in PlaygroundExamples of correct code for the { "allowLiterals": "in-intersections" }
option:
type Foo = { name: string } & { age: number };
Open in PlaygroundExamples of incorrect code for the { "allowLiterals": "in-unions-and-intersections" }
option:
type Foo = {};
type Foo = {
name: string;
age: number;
};
type Foo = {
name: string;
age: number;
walk: (miles: number) => void;
};
Open in PlaygroundExamples of correct code for the { "allowLiterals": "in-unions-and-intersections" }
option:
type Foo = { name: string } | { age: number };
type Foo = { name: string } & { age: number };
Open in PlaygroundallowMappedTypes
This applies to literal types.
The setting accepts the following values:
"always"
or"never"
to active or deactivate the feature."in-unions"
, allows aliasing in union statements, e.g.type Foo = string | string[];
"in-intersections"
, allows aliasing in intersection statements, e.g.type Foo = string & string[];
"in-unions-and-intersections"
, allows aliasing in union and/or intersection statements.
Examples of correct code for the { "allowMappedTypes": "always" }
options:
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] };
Open in PlaygroundExamples of incorrect code for the { "allowMappedTypes": "in-unions" }
option:
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] };
Open in PlaygroundExamples of correct code for the { "allowMappedTypes": "in-unions" }
option:
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] };
Open in PlaygroundExamples of incorrect code for the { "allowMappedTypes": "in-intersections" }
option:
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] };
Open in PlaygroundExamples of correct code for the { "allowMappedTypes": "in-intersections" }
option:
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] };
Open in PlaygroundExamples of incorrect code for the { "allowMappedTypes": "in-unions-and-intersections" }
option:
type Foo<T> = { readonly [P in keyof T]: T[P] };
type Foo<T> = { [P in keyof T]?: T[P] };
Open in PlaygroundExamples of correct code for the { "allowMappedTypes": "in-unions-and-intersections" }
option:
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] };
Open in PlaygroundallowTupleTypes
This applies to tuple types (type Foo = [number]
).
The setting accepts the following options:
"always"
or"never"
to active or deactivate the feature."in-unions"
, allows tuples in union statements, e.g.type Foo = [string] | [string, string];
"in-intersections"
, allows tuples in intersection statements, e.g.type Foo = [string] & [string, string];
"in-unions-and-intersections"
, allows tuples in union and/or intersection statements.
Examples of correct code for the { "allowTupleTypes": "always" }
options:
type Foo = [number];
type Foo = [number] | [number, number];
type Foo = [number] & [number, number];
type Foo = [number] | ([number, number] & [string, string]);
Open in PlaygroundExamples of incorrect code for the { "allowTupleTypes": "in-unions" }
option:
type Foo = [number];
type Foo = [number] & [number, number];
type Foo = [string] & [number];
Open in PlaygroundExamples of correct code for the { "allowTupleTypes": "in-unions" }
option:
type Foo = [number] | [number, number];
type Foo = [string] | [number];
Open in PlaygroundExamples of incorrect code for the { "allowTupleTypes": "in-intersections" }
option:
type Foo = [number];
type Foo = [number] | [number, number];
type Foo = [string] | [number];
Open in PlaygroundExamples of correct code for the { "allowTupleTypes": "in-intersections" }
option:
type Foo = [number] & [number, number];
type Foo = [string] & [number];
Open in PlaygroundExamples of incorrect code for the { "allowTupleTypes": "in-unions-and-intersections" }
option:
type Foo = [number];
type Foo = [string];
Open in PlaygroundExamples of correct code for the { "allowTupleTypes": "in-unions-and-intersections" }
option:
type Foo = [number] & [number, number];
type Foo = [string] | [number];
Open in PlaygroundallowGenerics
This applies to generic types, including TypeScript provided global utility types (type Foo = Record<string, number>
).
The setting accepts the following options:
"always"
or"never"
to active or deactivate the feature.
Examples of correct code for the { "allowGenerics": "always" }
options:
type Foo = Bar<string>;
type Foo = Record<string, number>;
type Foo = Readonly<Bar>;
type Foo = Partial<Bar>;
type Foo = Omit<Bar, 'a' | 'b'>;
Open in Playground