我在Typescript中注意到以下语法。
export type feline = typeof cat;
据我所知,type它不是内置的基本类型,也不是接口或类。实际上,它看起来更像是别名的语法,但是我找不到参考来验证我的猜测。
那么上面的陈述是什么意思呢?
Answers:
这是类型别名-用于为类型赋予其他名称。
在您的示例中,feline将是任何类型cat。
这是更完整的示例:
interface Animal {
legs: number;
}
const cat: Animal = { legs: 4 };
export type feline = typeof cat;
feline将是type Animal,您可以在任何地方将其用作类型。
const someFunc = (cat: feline) => {
doSomething();
};
export只需从文件中导出即可。与此相同:
type feline = typeof cat;
export {
feline
};
type Easing = "ease-in" | "ease-out" | "ease-in-out";