type GenericElement<T> = {
set: Setter<T>;
state: T;
}
type Setter<T> = (state: T) => void
type GenericElements = Array< GenericElement<string> | GenericElement<number>>
const genericElements = [{
set: (state: string) => console.log(state),
state: 'stateValue'
}, {
set: (state: number) => console.log(state),
state: 123
}]
genericElements.map(({set, state}) => set(state))
// ---------------------------------------|
/**
* Argument of type 'string | number' is not assignable to parameter of type 'never'.
* Type 'string' is not assignable to type 'never'.(2345)
*/
View the example on TS playground
Can this issue be resolved or is there another approach to handle the set(state)
operation without manual intervention for each child element or excessive code like
[() => set1(state1), () => setN(stateN)]
?