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
No error on unconstrained type parameter in > comparisonΒ #50603
The current behavior is clearly inconsistent. If you canβt use undefined in a greater than or less than check, you shouldnβt be able to use an unconstrained type parameter either.
changed the title [-]New error in TS 4.8: Object is possibly 'undefined' (after null check on `T | null`)[/-][+]No error on unconstrained type parameter in `>` comparison[/+]on Sep 2, 2022
Activity
jakebailey commentedon Sep 2, 2022
This is expected behavior in 4.8. See this section in the blog post: https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#unconstrained-generics-no-longer-assignable-to
You probably want to change your type variable to be constrained to
{}by writingT extends {}, or check for undefined.cakoose commentedon Sep 2, 2022
Ah, interesting! Where's the
{}requirement coming from, the comparison?For example, I'm confused why there's no error here:
jakebailey commentedon Sep 2, 2022
Honestly, I didn't even notice that the operator you used was
>. It does seem weird that the above compiles, yet, this does not.In that it's odd that we allow
>and<on an unconstrained type variable like this.andrewbranch commentedon Sep 2, 2022
The current behavior is clearly inconsistent. If you canβt use
undefinedin a greater than or less than check, you shouldnβt be able to use an unconstrained type parameter either.[-]New error in TS 4.8: Object is possibly 'undefined' (after null check on `T | null`)[/-][+]No error on unconstrained type parameter in `>` comparison[/+]ahejlsberg commentedon Jul 29, 2024
Note that the issue reported here also exists for the
+,-, and~unary operators. For example:With #59437, errors are reported in both cases above.