So I wish I could use an alias to an ugly type that looks like this:
Maybe<Promise<Paged<Carrier>, Problem>>[]
Something like:
import Response = Maybe<Promise<Paged<Carrier>, Problem>>[];
Is there a way to do type aliases in TypeScript?
“One difference is, that interfaces create a new name that is used everywhere. Type aliases don't create a new name — for instance, error messages won't use the alias name.”
A type alias is basically a name for any type. Type aliases can be used to represent not only primitives but also object types, union types, tuples and intersections.
In Typescript, Type assertion is a technique that informs the compiler about the type of a variable. Type assertion is similar to typecasting but it doesn't reconstruct code. You can use type assertion to specify a value's type and tell the compiler not to deduce it.
Interfaces are most recommended for defining new objects or methods or properties of an object where it will receive a specific component. Hence interface works better when using objects and method objects. Therefore it is our choice to choose between types or interface according to the program needs.
From version 1.4 Typescript supports type aliases (source).
Type Aliases
You can now define an alias for a type using the type keyword:
type PrimitiveArray = Array<string|number|boolean>; type MyNumber = number; type NgScope = ng.IScope; type Callback = () => void;
Type aliases are exactly the same as their original types; they are simply alternative names.
And from version 1.6 Typescript supports generic type aliases (source).
Generic type aliases
Leading up to TypeScript 1.6, type aliases were restricted to being simple aliases that shortened long type names. Unfortunately, without being able to make these generic, they had limited use. We now allow type aliases to be generic, giving them full expressive capability.
type switcharoo<T, U> = (u: U, t:T)=>T; var f: switcharoo<number, string>; f("bob", 4);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With