I'm trying to get return type based on exact string from parameter.
Here is what I have so far (without curring):
type Simple = {
(side: 'left'): boolean;
(side: 'right'): number;
};
const simple: Simple = (side: 'left' | 'right') =>
(side === 'left' ? random([true]) : random([1])) as boolean & number;
const simpleLeft = simple('left'); // boolean ✔️
const simpleRight = simple('right'); // number ✔️
Then I just add curring and it doesn't work anymore
type Hard = {
(): (side: 'left') => boolean;
(): (side: 'right') => number;
};
const hard: Hard = () => (side: 'left' | 'right') =>
(side === 'left' ? random([true]) : random([1])) as boolean & number;
const hardLeft = hard()('left'); // boolean ✔️
const hardRight = hard()('right'); // boolean ❌
random function is there to just get generic type like boolean instead of false or true
const random = <T>(array: T[]) =>
array[Math.floor(Math.random() * array.length)] as T;
Code above is just an example demonstrating problem, I can't just not use curring because it is a part of big code base