打字稿中的“输出类型”是什么?

我注意到类型脚本中的以下语法。

export type feline = typeof cat;

As far as I know, type is 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.

那么,上述陈述意味着什么呢?

127994 次浏览

这是一个 类型别名-它用于给类型赋予另一个名称。

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

这里有一个更成熟的例子:

interface Animal {
legs: number;
}


const cat: Animal = { legs: 4 };


export type feline = typeof cat;

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

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

export只是从文件中导出它:

type feline = typeof cat;


export {
feline
};

通过单独的类型和导出来理解它。

  1. 类型

类型文件类型文件

  1. 出口 出口