So, I'm building a TypeScript application and I'm having issues with generics and overload. Here's a minimal example of my code:
function isEqual(data: string, value: string): boolean;function isEqual(data: number, value: number): boolean;function isEqual(data: boolean, value: boolean): boolean;function isEqual(data: unknown, value: unknown): boolean { return data === value;}function compareVals<ValueType>( option: ValueType, value: ValueType,) { return isEqual(option, value); ^^^^^// No overload matches this call.// Overload 1 of 3, '(data: string, value: string): boolean', gave the following error.// Argument of type 'ValueType' is not assignable to parameter of type 'string'.// Overload 2 of 3, '(data: number, value: number): boolean', gave the following error.// Argument of type 'ValueType' is not assignable to parameter of type 'number'.// Overload 3 of 3, '(data: boolean, value: boolean): boolean', gave the following error.// Argument of type 'ValueType' is not assignable to parameter of type 'boolean'.}const equal = compareVals<number>(1,1); // equal: booleanconst equal2 = compareVals<boolean>(true, false); // equal2: boolean
I don't understand why TS doesn't use the same type.I also tried
...function compareVals<ValueType extends string | number | boolean>(...
to try forcing ValueType
as one of the overload types but it got the same error.
I feel that I'm missing something pretty basic here.