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
With this change, the ! operator only applies the NonNullable<T> type if T is an instantiable type (or union thereof) constrained to null or undefined (or unconstrained).
This does not, however, fix the underlying conceptual problem, which is that when you write
classX{fn(): void{// Type 'this' is not assignable to type 'NonNullable<this>'.// Type 'X' is not assignable to type 'NonNullable<this>'.constx: NonNullable<this>=this;// ~}}
One would expect the assignment to work, as the this type can never include null or undefined (barring shenanigans like X & undefined).
undefined is a value that this can legitimately have at runtime in strict mode code, though. But the --noImplicitThis mode probably catches most of the code that doesn't account for this.
@Kovensky AFAIK a class method we consider to have a strongly typed this in strict mode and don't allow calling it without one (and one compatable with the this type from the original declaration at that). The type of this is a method is an implicit type variable extending the containing class - such a definition does not allow for null or undefined values. It's the fact that it's a type parameter that makes it not work like you'd expect. If you tried to assign an X to a NonNullable<X>, it'd work fine because NonNullable<X> immediately simlifies to just X.
Thanks for your contribution. This PR has not been updated in a while and cannot be automatically merged at the time being. For housekeeping purposes we are closing stale PRs. If you'd still like to continue working on this PR, please leave a message and one of the maintainers can reopen it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
None yet
3 participants
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.
Fixes half of #22934
With this change, the
!operator only applies theNonNullable<T>type if T is an instantiable type (or union thereof) constrained tonullorundefined(or unconstrained).This does not, however, fix the underlying conceptual problem, which is that when you write
One would expect the assignment to work, as the
thistype can never includenullorundefined(barring shenanigans likeX & undefined).