Skip to content

consistently check expressions with >, >=, <, <= for unconstrained types in strictNullChecks - #59352

Closed
Isabel Duan (iisaduan) wants to merge 6 commits into
microsoft:mainfrom
iisaduan:unconstrainedTypeParamComparisonOnly
Closed

Isabel Duan (iisaduan) wants to merge 6 commits into
microsoft:mainfrom
iisaduan:unconstrainedTypeParamComparisonOnly

Conversation

@iisaduan

@iisaduan Isabel Duan (iisaduan) commented Jul 19, 2024 •

Copy link
Copy Markdown
Member

fixes #50603, which bisected to #49119

Previously: unconstrained type parameters were inconsistently checked in comparisons, as shown below (5.5.3 playground):

function f<T>(a: T, b: T) {
   return a > b; \\ okay
}

function g<T extends {} | undefined | null>(a: T, b: T) {
   return a > b; \\ error!! 
      \\ 'a' is possibly 'null' or 'undefined'.ts(18049)
      \\ 'b' is possibly 'null' or 'undefined'.ts(18049)
}

Now with this PR:

    function f1<T>(a: T, b: T): boolean {
        return a > b;
               ~
!!! error TS18046: 'a' is of type 'unknown'.
                   ~
!!! error TS18046: 'b' is of type 'unknown'.
    }
    
    function f2<T extends {} | undefined | null>(a: T, b: T): boolean {
        return a > b;
               ~
!!! error TS18049: 'a' is possibly 'null' or 'undefined'.
                   ~
!!! error TS18049: 'b' is possibly 'null' or 'undefined'.
    }    

This PR does not affect the checking of unconstrained types in other locations.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No error on unconstrained type parameter in > comparison

7 participants