Repository navigation
Type parameter explicitly constrained by unknown should not be assignable to {} #26796
Description
Activity
In the early days before
--strictNullChecks, the{}type was our top type. With--strictNullCheckswe carved outnullandundefinedand effectively our top type then became{} | null | undefined. However, for reasons of backward compatibility we kept{}as the default inference for an unconstrained type parameter and we kept the rule that allows an unconstrained type parameter to be assigned to{}.We now have a proper top type called
unknownand if it wasn't for backward compatibility we'd switch from{}tounknownin the situations above. However, it would be a significant breaking change.mattmccutchen commented
on Aug 31, 2018 ContributorAuthorMore actionsI would happily take the breaking change (under a new strict flag) for the soundness; in fact, I assumed it had come as part of
--strictNullChecksuntil I discovered this issue. (What was the specific reason that the change was unacceptable to make as part of--strictNullChecks, which was a large breaking change already?)If you don't want to do that, we could at least make a type parameter explicitly constrained by
unknownnot assignable to{}; nobody should be relying on that behavior. If we do that and then people use a lint rule to require every type parameter in their code to have a constraint (eitherunknown,{}, or something else), then we'll be in pretty good shape; the standard library may still have legacy unconstrained type parameters, but it seems unlikely to me that any of them will expose the buggy behavior. Shall I write the PR for the assignability change?Reacted by Jack Leigh, ulrichb, Jason Kuhrt, Spencer Park, NN, Milos Nedeljkovic, Junyoung/"Clare" Jang, John, Sindre Sorhus, movedoa and 8 more- changed the title
[-]Conditional type assumes type variable is assignable to `{}` but it might be null/undefined[/-][+]Type parameter explicitly constrained by `unknown` should not be assignable to `{}`[/+]on Aug 31, 2018 - addedSuggestionAn idea for TypeScriptAn idea for TypeScriptCommittedThe team has roadmapped this issueThe team has roadmapped this issue
on Sep 17, 2018 RyanCavanaugh commented
on Sep 17, 2018 MemberMore actionsPer notes in #26954, committed to trying it, at least
Reacted by movedoa and SlurpTheoReacted by six and Sean M. VieiraThe original issue here is actually not controversial. A type parameter with an explicit constraint of
unknownshouldn't be assignable to{}. We should just fix that.The larger issue is whether it is possible for us to switch the default constraint for type parameters to
unknownas we discussed in #26954. We should run that experiment and discuss.Reacted by SlurpTheoSo, is it planned for next release ?
Reacted by movedoa
TypeScript Version: master (2deb318)
Search Terms: conditional type variable parameter undefined null empty object constraint
Code
Expected behavior / Actual behavior: as marked
Playground Link: link (remember to enable
strictNullChecks)Related Issues: didn't find any
Discovered via https://stackoverflow.com/questions/52105268 .