I'm dealing with a TypeScript interface that consists of two properties (type:string
and args:object
). The contents of the args
property may vary based on the value of the type
. I'm looking for the correct type definition to specify for the args
so that the compiler and autocomplete can discern which properties are permissible in args
.
This situation resembles how Actions are used in Redux, where there is a type
and a payload
, and the reducer can determine the payload content through a switch statement. However, I am finding it challenging to implement this logic in my object.
I came across a helpful article at , but it specifically addresses a scenario with two interdependent method arguments rather than two distinct properties within the same object.
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
When defining an instance of IObject
with a type of "check", I would like the compiler/autocomplete to present the properties pertaining to IParametersCheck
.