在以下代码中:
function fourtyTwo<T extends boolean>(num: T): T extends true ? number : string {
if (num) {
return 42;
} else {
return 'fourty-two';
}
}
我得到2个相同错误的实例:
Type '42' is not assignable to type 'T extends true ? number : string'.ts(2322)
Type '"fourty-two"' is not assignable to type 'T extends true ? number : string'.ts(2322)
有没有一种方法可以在不转换返回类型的情况下使它正常工作?