Given the following code:
enum Option {
ONE = 'one',
TWO = 'two',
THREE = 'three',
}
type Value<T extends Option> = T extends Option.ONE
? string
: T extends Option.TWO
? number
: T extends Option.THREE
? boolean
: never
export const parseCellValue = async <T extends Option,
>(args: {
cellType: T
value: Value<T>
}) => {
if (args.value === null) return args.value
switch (args.cellType) {
case Option.ONE:
return (args.value).toString()
case Option.TWO:
// Property 'toFixed' does not exist on type 'string | number | boolean'.
// Property 'toFixed' does not exist on type 'string'.(2339)
return args.value.toFixed(2)
case Option.THREE:
return args.value
default:
throw new Error('Hmm')
}
}
TS errors because it infers the type of args.value to be string | number | boolean
I expect args.value to get a stricter type inference (case Option.ONE should only be string)
I tried an if statement and it's the same issue