Does TypeScript allow type aliases?

TypescriptAlias

Typescript Problem Overview


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?

Typescript Solutions


Solution 1 - Typescript

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; > 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 = (u: U, t:T)=>T; > var f: switcharoo; > f("bob", 4);

Solution 2 - Typescript

TypeScript supports imports, e.g.:

module A {
    export class c {
        d: any;
     }
}
    
module B {
    import moduleA = A;

    var e: moduleA.c = new moduleA.c();
}

module B2 {
    import Ac = A.c;

    var e: Ac = new Ac();
}
Update 1

Since TS 1.4 we can use type declarations:

type MyHandler = (myArgument: string) => void;

var handler: MyHandler;

Since TS 1.6 we can use local type declarations:

function f() {
    if (true) {
        interface T { x: number }
        let v: T;
        v.x = 5;
    }
    else {
        interface T { x: string }
        let v: T;
        v.x = "hello";
    }
}

Solution 3 - Typescript

A poor man's solution is to declare a dummy variable (e.g. t) with the desired type and use typeof t instead of the long type expression:

var t: { (x: number, f: { (foo: string, bar:boolean): void }): void };

var f: typeof t; var g: typeof t;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTrident D&#39;GaoView Question on Stackoverflow
Solution 1 - TypescriptMariusz PawelskiView Answer on Stackoverflow
Solution 2 - TypescriptTSVView Answer on Stackoverflow
Solution 3 - TypescriptMartin JambonView Answer on Stackoverflow