was wondering if there is any way for typescript to realise the type of the object by deriving it from the base control descendants
interface Config {
name: string;
}
interface Input extends Config {
placeholder?: string;
}
interface Select extends Config {
options: Array<Option>;
}
const fields: Array<Config> = [
{ name: 'a', placeholder: 'a'}, // placeholder does not exist in type 'config'
{ name: 'b', options: []} // options does not exist in type 'config'
]
also attempted something like
interface Config<T> {
name: string;
fieldType: T
}
interface Input extends Config<'input'> {
placeholder?: string;
}
can anyone redirect?
fieldsat all? You can writeconst fields = [ {name: 'a', placeholder: 'a'}, {name: 'b', options: []} ]and get an object which will be accepted by anything that requires a value of typeArray<Config>. Do you need to guarantee that each element offieldsconforms to one of your declared extensions ofConfig? If so, you'll need a union of those types somewhere in your code, which is what @ThomasThiebaud's answer suggests, but you say you want to avoid that.