Disallow the use of type aliases (no-type-alias)
In TypeScript, type aliases serve three purposes:
Aliasing other types so that we can refer to them using a simpler name.
Act sort of like an interface, providing a set of methods and properties that must exist in the objects implementing the type.
Act like mapping tools between types to allow quick modifications.
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.
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.
Rule Details
This rule disallows the use of type aliases in favor of interfaces and simplified types (primitives, tuples, unions, intersections, etc).
Options
This rule, in its default state, does not require any argument. If you would like to enable one or more of the following you may pass an object with the options set as follows:
allowAliases
set to"always"
will allow you to do aliasing (Defaults to"never"
).allowCallbacks
set to"always"
will allow you to use type aliases with callbacks (Defaults to"never"
)allowConditionalTypes
set to"always"
will allow you to use type aliases with conditional types (Defaults to"never"
)allowConstructors
set to"always"
will allow you to use type aliases with constructors (Defaults to"never"
)allowLiterals
set to"always"
will allow you to use type aliases with literal objects (Defaults to"never"
)allowMappedTypes
set to"always"
will allow you to use type aliases as mapping tools (Defaults to"never"
)allowTupleTypes
set to"always"
will allow you to use type aliases with tuples (Defaults to"never"
)
allowAliases
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:
Examples of incorrect code for the { "allowAliases": "in-unions" }
option:
Examples of correct code for the { "allowAliases": "in-unions" }
option:
Examples of incorrect code for the { "allowAliases": "in-intersections" }
option:
Examples of correct code for the { "allowAliases": "in-intersections" }
option:
Examples of incorrect code for the { "allowAliases": "in-unions-and-intersections" }
option:
Examples of correct code for the { "allowAliases": "in-unions-and-intersections" }
option:
allowCallbacks
allowCallbacks
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:
allowConditionalTypes
allowConditionalTypes
This applies to conditional types.
Examples of correct code for the { "allowConditionalTypes": "always" }
option:
allowConstructors
allowConstructors
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:
allowLiterals
allowLiterals
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:
Examples of incorrect code for the { "allowLiterals": "in-unions" }
option:
Examples of correct code for the { "allowLiterals": "in-unions" }
option:
Examples of incorrect code for the { "allowLiterals": "in-intersections" }
option:
Examples of correct code for the { "allowLiterals": "in-intersections" }
option:
Examples of incorrect code for the { "allowLiterals": "in-unions-and-intersections" }
option:
Examples of correct code for the { "allowLiterals": "in-unions-and-intersections" }
option:
allowMappedTypes
allowMappedTypes
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:
Examples of incorrect code for the { "allowMappedTypes": "in-unions" }
option:
Examples of correct code for the { "allowMappedTypes": "in-unions" }
option:
Examples of incorrect code for the { "allowMappedTypes": "in-intersections" }
option:
Examples of correct code for the { "allowMappedTypes": "in-intersections" }
option:
Examples of incorrect code for the { "allowMappedTypes": "in-unions-and-intersections" }
option:
Examples of correct code for the { "allowMappedTypes": "in-unions-and-intersections" }
option:
allowTupleTypes
allowTupleTypes
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:
Examples of incorrect code for the { "allowTupleTypes": "in-unions" }
option:
Examples of correct code for the { "allowTupleTypes": "in-unions" }
option:
Examples of incorrect code for the { "allowTupleTypes": "in-intersections" }
option:
Examples of correct code for the { "allowTupleTypes": "in-intersections" }
option:
Examples of incorrect code for the { "allowTupleTypes": "in-unions-and-intersections" }
option:
Examples of correct code for the { "allowLiterals": "in-unions-and-intersections" }
option:
When Not To Use It
When you can't express some shape with an interface or you need to use a union, tuple type, callback, etc. that would cause the code to be unreadable or impractical.
Further Reading
Related to
TSLint: interface-over-type-literal
Last updated