【发布时间】:2020-01-01 03:27:33
【问题描述】:
如果我有一个看起来有点像这样的类型的集合,只会更冗长:
type ValidValues = string | number | null
type ValidTypes = "text" | "time" | "unknown"
type Decorated = {
name?: string | null
type?: ValidTypes
value?: ValidValues
title: string
start: number
}
type Injected = {
extras: object
}
// overriding the types from Decorated
type Text = Decorated & Injected & {
name: string
type: "text"
value: string
}
我的实际代码还有更多内容,但这显示了核心思想。我不想相信自己才能使类型之间的关系恰到好处。毕竟类型代数,我希望工具向我展示Text“评估”的类型定义。
所以对于上面的例子,我希望Text 中指定的字段将覆盖Decorated 类型中的先前声明,并且我假设的工具提示的输出将(我希望)向我展示类似这样的内容:
{
name: string
type: "text"
value: string
title: string
start: number
extras: object
}
有没有什么方便的方法可以得到这些信息?
【问题讨论】:
标签: typescript visual-studio-code algebraic-data-types