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)]?

3
  • Does this answer your question? How do I type parameterize a tuple? Commented Jul 15, 2020 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<string>|Foo<number>>, one would end up with Array<FooExistentiallyQuantified>. Does it actually work if one has Foo<string>|Foo<number>? It seems that it cannot use existentialize-like nat trafo inside of the map, so one is forced to cast Foo<string>|Foo<number> to something like Foo<any>, and at that point the whole previous construction seems to become unnecessary. Are there better solutions? Commented Jul 15, 2020 at 21:28
  • 1
    @AndreyTyukin Hmm, you're right. I'm not sure anything quite works for an actual Array<Foo<string>|Foo<number>>. 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. Commented Jul 15, 2020 at 21:41

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.