【发布时间】:2020-12-10 03:51:23
【问题描述】:
我需要创建一个具有多个属性的类型,其中每个属性都有自己的类型(即不能保证所有属性都具有相同的类型。我还需要确保所有属性名称都严格来自另一个字符串联合类型.
这是我需要实现的示例
type Operations = "create:op" | "update:op" | "delete:op";
// Next three type defs only to demonstrate that all of them are different and
// they don't have any common ancestor, etc.
type CreateOperationParams = {
foo: string
}
type UpdateOperationParams = {
bar: boolean
}
type DeleteOperationParams = {
baz: number
}
// All properties in this type must be from Operations
// and every item from Operations must be present here
// as a property
type OperationParams = {
"create:op"?: CreateOperationParams | false
"update:op"?: UpdateOperationParams | false
"delete:op"?: DeleteOperationParams | false
}
如果OperationsParams完全是手动编码的,那么很容易犯这样的错误
type OperationParams = {
// Property name is not one of Operations, a dot is used instead of a colon.
// This case must raise TS error. Another error should be that the correct
// value "create:op" is not present in type properties
"create.op"?: CreateOperationParams | false
...
}
以上是我实际拥有的简化示例。我在Operations 中输入了大约 40 个不同的值,其中一些名称有点长,带有一些特殊字符,如冒号或点。我显然可以小心地将它们全部复制并粘贴到 OperationsParams 类型中,并为每个类型定义正确的类型,但出错的可能性非常高,尤其是当 Operations 更改时。
有什么方法可以实现我的场景,但强制OperationsParams 的每个属性都来自Operations 联合类型,并且不能添加其他属性?还要确保Operations 中的所有项目都作为类型属性出现在OperationsParams 中?
【问题讨论】:
标签: typescript