You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generic bounds not enforced on generic argument #32330
functionf1<T>(o: T){o.toString();// error// This is expected, because T might be undefined or null}functionf2<Textends{}>(o: T){o.toString();// no error// This is expected, this is the correct way to fix the above function.}functionuser<T>(t: T){f1(t);// Allowed, but irrelevant because f1() is invalidf2(t);// Allowed? <- this is the bugt.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.
Is an old special assignability rule case left over from when the default for an unconstrained type parameter was {} instead of unknown, I believe. I think it's just a matter of deleting these lines:
if(!constraint||(source.flags&TypeFlags.TypeParameter&&constraint.flags&TypeFlags.Any)){// A type variable with no constraint is not related to the non-primitive object type.if(result=isRelatedTo(emptyObjectType,extractTypesOfKind(target,~TypeFlags.NonPrimitive))){errorInfo=saveErrorInfo;returnresult;}}
in checker.ts - but the impact of doing so will need to be checked, ofc.
Activity
evmar commentedon Jul 10, 2019
Perhaps a simpler expression of this is
Which I also expect should fail.
RyanCavanaugh commentedon Jul 10, 2019
Wesley Wigham (@weswigham) ?
weswigham commentedon Jul 10, 2019
Is an old special assignability rule case left over from when the default for an unconstrained type parameter was
{}instead ofunknown, I believe. I think it's just a matter of deleting these lines:in
checker.ts- but the impact of doing so will need to be checked, ofc.13 remaining items