@typescript-eslint/no-type-alias
Disallow type aliases, or allow only some kinds of them
Also called typescript-eslint/no-type-alias, typescript/no-type-alias
Some teams want object shapes written as interface and other types written out where they're used. This rule bans type aliases, and its options allow the kinds you still need.
With no options, every type declaration is reported. In a union or an intersection, each member is reported separately.
typescript-eslint has deprecated this rule.
Examples#
type UserId = stringType aliases are not allowed.;
type User = {Type literals are not allowed.
id: string;
};
type OnSave = (user: User) => voidType callbacks are not allowed.;interface User {
id: string;
}
interface OnSave {
(user: User): void;
}Options#
Each option allows one kind of alias. It's "never" (default) or "always".
| Option | What it allows |
|---|---|
allowAliases | Another name for a type: type Id = string, type Mode = "on", type Ids = string[], type Keys = keyof User. |
allowLiterals | Object types: type User = { id: string }. |
allowMappedTypes | Mapped types: type Flags = { [K in Keys]: boolean }. |
allowTupleTypes | Tuples: type Point = [number, number]. |
allowCallbacks | Function types: type OnSave = () => void. |
allowConstructors | Constructor types: type Ctor = new () => User. |
allowConditionalTypes | Conditional types: type Item<T> = T extends (infer U)[] ? U : T. |
allowGenerics | A type with type arguments: type Users = Map<string, User>. |
The first four also take "in-unions", "in-intersections" and "in-unions-and-intersections". These allow that kind only as a member of a union, of an intersection, or of either.
A common setup allows unions of names and literals, which an interface can't express:
{ "allowAliases": "in-unions" }No problemstype Status = "draft" | "published" | "archived";An alias that isn't a union is still reported:
{ "allowAliases": "in-unions" }1 problemtype Status = "draft"Type aliases are not allowed.;When not to use it#
Most TypeScript code uses aliases for unions, tuples, mapped types and conditional types, which you can't write any other way. If you only want interface instead of type for object shapes, use @typescript-eslint/consistent-type-definitions.