-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeBugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Description
TypeScript Version: [email protected], 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:
dragomirtitian, danvk, MaxNanasy and SlurpTheo
Metadata
Metadata
Assignees
Labels
Breaking ChangeWould introduce errors in existing codeWould introduce errors in existing codeBugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone