'Why the passed in parameter type is by default the value of generic type
I saw a generic function like this:
const myFun = <T>(p: keyof T extends 'foo' ? never: T)=> {
console.log(`...`);
}
myFun('foo') // good
myFun({foo: 1}); // bad: Type 'number' is not assignable to type 'never'
I have one question:
When calling myFun, I only pass in either string or object, I don't explicitly tell typescript the value of generic type T like myFun<string>(...), it seems the function is already considering the passed in parameter type as the value for T & starts evaluating the condition keyof T extends 'foo', I wonder why is that? Is that the default behavior of typescript that the argument type is by default the value of generic T?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
