Closed
Description
TypeScript Version: master
Search Terms: generic constrained type parameter error
Code
People are regularly confused why code like this doesn't work:
function fn1<T extends string>(x: T): T {
return "hello world!";
}
function fn2<T extends HTMLElement>(obj: T): T[] {
return [obj, new HTMLDialogElement()];
}
function fn3<T extends boolean>(obj: T = false) {
}
Actual behavior: Unelaborated error like "Can't assign false
to T
"
Expected behavior:
If the source type is assignable to the constraint of a type parameter, we should issue a more specific error that attempts to explain the situation:
🚲 🏠
Type "false" is not assignable to type "T" "false" is assignable to the constraint type "boolean", but "T" could be a different more-specific type during a call
Playground Link: Link