Javascript 什么是 Typescript 中的“导出类型”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44079820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:20:01  来源:igfitidea点击:

What is `export type` in Typescript?

javascripttypescript

提问by hackjutsu

I notice the following syntax in Typescript.

我注意到 Typescript 中的以下语法。

export type feline = typeof cat;

As far as I know, typeis not a built-in basic type, nor it is an interface or class. Actually it looks more like a syntax for aliasing, which however I can't find reference to verify my guess.

据我所知,type不是内置的基本类型,也不是接口或类。实际上它看起来更像是别名的语法,但是我找不到参考来验证我的猜测。

So what does the above statement mean?

那么上面的说法是什么意思呢?

回答by James Monger

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

这是一个类型别名- 它用于为类型提供另一个名称。

In your example, felinewill be the type of whatever catis.

在您的示例中,feline将是任何类型cat

Here's a more full fledged example:

这是一个更完整的示例:

interface Animal {
    legs: number;
}

const cat: Animal = { legs: 4 };

export type feline = typeof cat;

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

feline将是 type Animal,您可以将其用作任何您喜欢的类型。

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

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

export只需从文件中导出它。这与执行此操作相同:

type feline = typeof cat;

export {
    feline
};