What is `export type` in Typescript?

后端 未结 1 1289
轮回少年
轮回少年 2021-01-31 06:55

I notice the following syntax in Typescript.

export type feline = typeof cat;

As far as I know, type is not a built-in basic type,

1条回答
  •  伪装坚强ぢ
    2021-01-31 07:20

    This is a type alias - it's used to give another name to a type.

    In your example, feline will be the type of whatever cat is.

    Here's a more full fledged example:

    interface Animal {
        legs: number;
    }
    
    const cat: Animal = { legs: 4 };
    
    export type feline = typeof cat;
    

    feline will be the type Animal, and you can use it as a type wherever you like.

    const someFunc = (cat: feline) => {
        doSomething();
    };
    

    export simply exports it from the file. It's the same as doing this:

    type feline = typeof cat;
    
    export {
        feline
    };
    

    0 讨论(0)
提交回复
热议问题