A Type Alias is used to create a new name for a type. Type aliases can be used to create a shorthand for complex types or define a union type.
Example:
type Name = string;
type NameResolver = () => string;
type NameOrResolver = Name | NameResolver;
function getName(n: NameOrResolver): Name {
if (typeof n === "string") {
return n;
} else {
return n();
}
}
const name = getName("John Doe");
const resolvedName = getName(() => "Jane Doe");
Here:
Name
is a type alias forstring
.NameResolver
is a type alias for a function that returns a string.NameOrResolver
is a union type alias, combiningName
andNameResolver
.