【发布时间】:2020-10-26 10:55:31
【问题描述】:
考虑这个例子:
const FIRST = "FIRST"
const SECOND = "SECOND"
type TOptions = FIRST | SECOND
const someFunction = (options: TOptions): void => {
// do something, no return
}
我喜欢在类型声明中使用 const 值
type TOptions = FIRST | SECOND // not working
限制选择范围。这些 const 在整个项目中使用,因此不使用它们并像这样键入它是不明智的:
type TOptions = "FIRST" | "SECOND" // working, as expected
我不想重复自己,我喜欢使用 const 值作为类型联合选项。
我该怎么做?
【问题讨论】:
标签: javascript typescript