I'm trying to infer some types for working with a combobox component.
Basically, given a type T
, the options should be T[]
, and an onChange handler should be (value: T) => void
. However, if the isMulti
flag is true, then onChange should be (value: T[]) => void
. I'm just unsure as to how to configure the overloading types to get this to work properly:
type Options<T = any> = {
options: T[]
} & ({
isMulti?: false
onChange: (value: T) => void
} | {
isMulti: true
onChange: (value: T[]) => void
})
interface Option {
value: string,
}
const a: Options<Option> = {
// isMulti: false,
options: [{
value: 'abc',
}],
onChange: (value) => console.log(value)
}
Basically the issue is that if isMulti
is undefined, then the value in onChange is any
!
Parameter 'value' implicitly has an 'any' type.
Is there any way to do this, or do I need to make isMulti
required?