108

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?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
  • As ryan answered. One thing missing though is operator overloading, so you cannot extend something like number – basarat Feb 17 '14 at 22:21
  • 1
    3 days ago typescript introduced type aliases: http://blogs.msdn.com/b/typescript/archive/2014/11/18/what-s-new-in-the-typescript-type-system.aspx – hidden Jan 20 '15 at 07:34
  • http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-aliases – chharvey Sep 30 '18 at 21:43

3 Answers3

147

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);
Mariusz Pawelski
  • 25,983
  • 11
  • 67
  • 80
  • Type aliases in 1.4 don't support type parameters which renders them mostly useless at least in my setting where most of things are generic. There is another request in discussion for adding type parameters to aliases: https://github.com/Microsoft/TypeScript/issues/1616 Hopefully will be enough interest generated for this featrure, so it gets pushed to life. – Trident D'Gao Feb 05 '15 at 14:22
  • 3
    Per the comment from @AlekseyBykov — generic type aliases are now live! See https://github.com/Microsoft/TypeScript/pull/3397 – brynb Jun 12 '15 at 18:12
  • I find these type aliases to be useless, since you cannot use them to construct instances. This fails: `type MyMap = Map; let map = new MyMap();` – jlh Aug 16 '18 at 11:20
  • @jlh Well, type aliases (and types in general) are erased after compilation. So to make it work as you want TS would need to rewrite you `new` call or generate some code for type alias. And this in general is against TS philosophy and [design goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals). You can always create you own `MyMap` class or function that wraps underlying `Map;` – Mariusz Pawelski Aug 16 '18 at 12:30
  • @MariuszPawelski Yes I ended up wrapping it, but it's a pain if the underlying type has a lot of methods you want to use. It would be trivial if it was possible to just extend these types, but that doesn't work either, at least it doesn't for `Map`, there's an open issue for that. – jlh Aug 16 '18 at 14:48
11

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";
    }
}
TSV
  • 7,538
  • 1
  • 29
  • 37
9

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;
Martin Jambon
  • 4,629
  • 2
  • 22
  • 28