Temporarily revert unconstrained type parameter strictness in TS 4.7 #48923
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR reverts #48366.
We've seen that the change to disallow a generic
T
to be assignable to{}
are particularly disruptive to existing codebases. Some examples of where this goes awry:{}
object
{ all?: string, optional?: number, objectTypes?: boolean }
We've heard about or saw examples of these on Definitely Typed, Visual Studio Code, and internal codebases at Microsoft, Google, and Bloomberg. In particular, @amcasey and I were surprised to find out about the all-optional-objects break, and it sounds like @dragomirtitian and @robpalme might be dealing with that one heavily.
Definitely Typed has already been updated which should help a bit; however, I think that we have other avenues to make this change more approachable.
First, we could break the breakages across versions to make it easier to upgrade. We could narrow the previous exception on unconstrained generics so that an unconstrained
T
is assignable to strictly{}
andobject
, or strictly not{}
andobject
. This would allow users to only focus on these cases, or the all-optional-object-type break.Additionally, @ahejlsberg has been thinking about alternative ways to do narrowing on generics. We would pull that in now, but it's not well-understood in the context of the broader ecosystem. That will have to wait until 4.8.
If we bring this in, my advice for people who may have a pending upgrade is to keep their changes in place. The new constraint enforcement is still more correct, and we'd like to bring it in to a newer version of TS anyway. (CC @frigus02)