I am looking to illustrate the concept that a property of an object is "in sync" with another property within the same object.
Consider a hypothetical type called Widget, which represents a UI component storing a specific data type.
type Widget = {
id: string
type: string
}
const datepicker: Widget = {
id: "datepicker",
type: "string" // stores an ISO string
}
const numberpicker: Widget = {
id: "numberpicker",
type: "number"
}
Now, let's introduce a Config object, which specifies that certain inputs have a particular type and are supported by a specific widget. The goal is to ensure that only compatible widgets are used.
type ConfigItem = {
name: string
type: string
widget: Widget
}
type Config = Array<ConfigItem>;
const config: Config = [{
name: "birthdate",
type: "string",
widget: datepicker, // This is valid because a datepicker supports a string
}, {
name: "age",
type: "number",
widget: numberpicker // This is valid because a numberpicker supports a number
}, {
name: "numberPets",
type: "number",
widget: datepicker // This is invalid as a datepicker cannot support a number
}]
I am uncertain about how to properly articulate this requirement or if it is feasible... I aim to generalize this idea beyond just this example. Thank you.