[英]Typescript: Type of a property dependent on another property within the same object
I have a TypeScript interface with two properties ( type:string
and args:object
).我有一个带有两个属性的 TypeScript 接口(
type:string
和args:object
)。 The args
may have different properties depending on the type
. args
可能具有不同的属性,具体取决于type
。 What type definition to I need to apply to args
so the compiler/autocomplete will know which properties are allowed for args
?我需要对
args
应用什么类型定义,以便编译器/自动完成将知道args
允许哪些属性?
This is somewhat similar to how I use Actions in Redux, which do have a type
and a payload
and in my reducer the compiler knows by the switch-statement what the payload contains.这有点类似于我在 Redux 中使用 Actions 的方式,它有一个
type
和一个payload
,在我的减速器中,编译器通过 switch 语句知道有效负载包含什么。 But I can't get this to work with my object.但我不能让它与我的对象一起工作。 I have read an excellent article here https://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/ but this describes the problem for a method with two args which depend on one another but not how to get this working for two properties within the same object.
我在这里阅读了一篇很棒的文章https://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/但这描述了具有两个相互依赖的 args 的方法的问题,但是不是如何让这对同一对象中的两个属性起作用。
export interface IObject {
type: ObjectType
parameters: ObjectParameters
}
export type ObjectType = "check" | "counter"
export interface IParametersCheck {
checked: boolean
}
export interface IParametersCounter {
max: number
min: number
step: number
}
export type ObjectParameters = IParametersCheck | IParametersCounter
If I have an IObject
and set the type to "check" the compiler/autocomplete should offer the properties for IParametersCheck
.如果我有一个
IObject
并将类型设置为“检查”,编译器/自动完成应该提供IParametersCheck
的属性。
I think what you are actually looking for is a discriminated union.我认为您实际上正在寻找的是一个受歧视的工会。
IObject
should itself be a union: IObject
本身应该是一个联合:
export type IObject = {
type: "checked"
parameters: IParametersCheck
} | {
type: "counter"
parameters: IParametersCounter
}
export type ObjectType = IObject['type'] //// in case you need this union
export type ObjectParameters = IObject['parameters'] //// in case you need this union
export interface IParametersCheck {
checked: boolean
}
export interface IParametersCounter {
max: number
min: number
step: number
}
You could also do it with conditional types but I think the union solution works better :你也可以用条件类型来做,但我认为联合解决方案效果更好:
export interface IObject<T extends ObjectType> {
type: T
parameters: T extends 'checked' ? IParametersCheck: IParametersCounter
}
Or with a mapping interface:或者使用映射接口:
export interface IObject<T extends ObjectType> {
type: T
parameters: ParameterMap[T]
}
type ParameterMap ={
'checked': IParametersCheck
'counter': IParametersCounter
}
export type ObjectType = keyof ParameterMap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.