Does TypeScript allow type aliases?

后端 未结 3 1437
广开言路
广开言路 2020-12-13 23:27

So I wish I could use an alias to an ugly type that looks like this:

Maybe, Problem>>[]

Something

3条回答
  •  庸人自扰
    2020-12-14 00: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";
        }
    }
    

提交回复
热议问题