Closed
Description
TypeScript Version: typescript@3.6.0-dev.20190710, 3.5.1
Search Terms: generic bounds toString
Code
function f1<T>(o: T) {
o.toString(); // error
// This is expected, because T might be undefined or null
}
function f2<T extends {}>(o: T) {
o.toString(); // no error
// This is expected, this is the correct way to fix the above function.
}
function user<T>(t: T) {
f1(t); // Allowed, but irrelevant because f1() is invalid
f2(t); // Allowed? <- this is the bug
t.toString(); // Error, expected for the same reason as f1()
}
Expected behavior:
You should not be able to pass an arbitrary T into a function that demands T extends {}
.
Actual behavior:
Despite .toString()
being disallowed both in user() and in f1(), you can just pass it to f2() and still call toString
on the same value.
Related Issues: