3
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)
*/

Example on TS playground

Is it possible to implement it without this issue, or how to lead proposed structure to be abble to hanlde it set(state) in common way without boilerplates like perform this operation for each child manually or overcodes like [() => set1(state1), () => setN(stateN)]?

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
eLeontev
  • 43
  • 3
  • Does this answer your question? [How do I type parameterize a tuple?](https://stackoverflow.com/questions/46185023/how-do-i-type-parameterize-a-tuple) – HTNW Jul 15 '20 at 14:12
  • @HTNW In the answer you linked, you ended up pushing the universal quantification inside of the array, i.e. instead of `Array|Foo>`, one would end up with `Array`. Does it actually work if one has `Foo|Foo`? It seems that it cannot use `existentialize`-like nat trafo inside of the `map`, so one is forced to cast `Foo|Foo` to something like `Foo`, and at that point the whole previous construction seems to become unnecessary. Are there better solutions? – Andrey Tyukin Jul 15 '20 at 21:28
  • 1
    @AndreyTyukin Hmm, you're right. I'm not sure anything quite works for an actual `Array|Foo>`. I think we basically need a way to say that, since the same function typechecks on both branches, it should typecheck on the union. Right now the compiler is destroying the "correlation" between `set` and `state`. Close vote retraced. – HTNW Jul 15 '20 at 21:41

1 Answers1

0

After of continious invastigation I found the way which can work for me.

Link to TS playground to play.

enum genericTypes {
    stringTypes = 'stringTypes',
    numberTypes = 'numberTypes',
}
const {numberTypes, stringTypes} = genericTypes;

type GenericElement<T extends genericTypes> = {
    set: Setter<T>;
    state: State<T>;
};

type State<T extends genericTypes> = T extends genericTypes.numberTypes ? number : string;

type States = {
    [numberTypes]: State<genericTypes.numberTypes>;
    [stringTypes]: State<genericTypes.stringTypes>;
};

type Setter<T extends genericTypes> = (state: State<T>) => void;

type Setters = {
    [numberTypes]: Setter<genericTypes.numberTypes>;
    [stringTypes]: Setter<genericTypes.stringTypes>;
};

const setters: Setters = {
    [numberTypes]: (state: State<genericTypes.numberTypes>) => console.log(state),
    [stringTypes]: (state: State<genericTypes.stringTypes>) => console.log(state),
};

const states: States = {
    [numberTypes]: 123,
    [stringTypes]: '123',
};

type GenericElements = Array<GenericElement<genericTypes>>;

const genericElements: GenericElements = [
    { set: setters.numberTypes, state: states.numberTypes },
    { set: setters.stringTypes, state: states.stringTypes },
];

genericElements.map(({ set, state }) => set(state));

I will continue to investigate to finally understanding. If anybody could review the proposal with feedback I will be very appreciate.

eLeontev
  • 43
  • 3