I am newbie in TS and need some help. I've got really weird behavior of TS. I try to make a map of arrow functons and then call them by their name. Look at this
const first = (x: string): string => x.toUpperCase();
const second = (x: number): number => x ** 2
type FirstType = typeof first;
type SecondType = typeof second;
type MyUnion = FirstType | SecondType;
const handlers = new Map<string, MyUnion>();
handlers.set('first', first);
handlers.set('second', second);
// here myHandler has this type, as predicted:
// MyUnion | undefined
const myHandler = handlers.get('first');
if (myHandler) {
// but here myHandler somehow has this type:
// (x: never) => string | number
// that's why i got this error
// Argument of type 'number' is not assignable to parameter of type 'never'.(2345)
const a = myHandler(10)
// Argument of type 'string' is not assignable to parameter of type 'never'.(2345)
const b = myHandler('asd')
}
Could enybody explain what's going on here? Why TS has changed type of the value of the map?