-
Notifications
You must be signed in to change notification settings - Fork 12.8k
consistently check expressions with >, >=, <, <= for unconstrained types in strictNullChecks #59352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iisaduan
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
iisaduan:unconstrainedTypeParamComparisonOnly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee33225
add check for >, >=, <, <= for unconstrained types
iisaduan fca2ca0
fmt
iisaduan 1fa487e
change error to `possibly null or undefined`
iisaduan 0308dde
add intersections
iisaduan 43c24bb
add tests, fix comment
iisaduan 529331b
format
iisaduan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
109 changes: 109 additions & 0 deletions
109
tests/baselines/reference/unconstrainedTypeComparison.errors.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
unconstrainedTypeComparison.ts(2,12): error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(2,16): error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(6,12): error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(6,16): error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(10,12): error TS18046: 'a' is of type 'unknown'. | ||
unconstrainedTypeComparison.ts(10,16): error TS18046: 'b' is of type 'unknown'. | ||
unconstrainedTypeComparison.ts(14,12): error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(14,16): error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(18,12): error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(18,16): error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(22,12): error TS18046: 'a' is of type 'unknown'. | ||
unconstrainedTypeComparison.ts(22,16): error TS18046: 'b' is of type 'unknown'. | ||
unconstrainedTypeComparison.ts(26,12): error TS18048: 'a' is possibly 'undefined'. | ||
unconstrainedTypeComparison.ts(26,16): error TS18048: 'b' is possibly 'undefined'. | ||
unconstrainedTypeComparison.ts(30,12): error TS18047: 'a' is possibly 'null'. | ||
unconstrainedTypeComparison.ts(30,16): error TS18047: 'b' is possibly 'null'. | ||
unconstrainedTypeComparison.ts(34,12): error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(34,16): error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
unconstrainedTypeComparison.ts(45,12): error TS18047: 'a' is possibly 'null'. | ||
unconstrainedTypeComparison.ts(45,16): error TS18048: 'b' is possibly 'undefined'. | ||
|
||
|
||
==== unconstrainedTypeComparison.ts (20 errors) ==== | ||
function f1<T>(a: T, b: T): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
~ | ||
!!! error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
} | ||
|
||
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'. | ||
} | ||
|
||
function f3<T extends unknown>(a: T, b: T): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18046: 'a' is of type 'unknown'. | ||
~ | ||
!!! error TS18046: 'b' is of type 'unknown'. | ||
} | ||
|
||
function f4<T, U extends T>(a: U, b: U): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
~ | ||
!!! error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
} | ||
|
||
function f5<T extends {} | undefined | null, U extends T>(a: U, b: U): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
~ | ||
!!! error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
} | ||
|
||
function f6<T extends unknown, U extends T>(a: U, b: U): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18046: 'a' is of type 'unknown'. | ||
~ | ||
!!! error TS18046: 'b' is of type 'unknown'. | ||
} | ||
|
||
function f7<T extends {} | undefined, U extends T>(a: U, b: U): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18048: 'a' is possibly 'undefined'. | ||
~ | ||
!!! error TS18048: 'b' is possibly 'undefined'. | ||
} | ||
|
||
function f8<T extends {} | null, U extends T>(a: U, b: U): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18047: 'a' is possibly 'null'. | ||
~ | ||
!!! error TS18047: 'b' is possibly 'null'. | ||
} | ||
|
||
function f9<T extends undefined | null, U extends T>(a: U, b: U): boolean { | ||
return a > b; | ||
~ | ||
!!! error TS18049: 'a' is possibly 'null' or 'undefined'. | ||
~ | ||
!!! error TS18049: 'b' is possibly 'null' or 'undefined'. | ||
} | ||
|
||
|
||
function compare<T>(a: T, b: T): boolean { | ||
if (a === undefined) { | ||
return false; | ||
} | ||
if (b === null) { | ||
return false; | ||
} | ||
return a > b; | ||
~ | ||
!!! error TS18047: 'a' is possibly 'null'. | ||
~ | ||
!!! error TS18048: 'b' is possibly 'undefined'. | ||
} |
156 changes: 156 additions & 0 deletions
156
tests/baselines/reference/unconstrainedTypeComparison.symbols
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
//// [tests/cases/compiler/unconstrainedTypeComparison.ts] //// | ||
|
||
=== unconstrainedTypeComparison.ts === | ||
function f1<T>(a: T, b: T): boolean { | ||
>f1 : Symbol(f1, Decl(unconstrainedTypeComparison.ts, 0, 0)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 0, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 0, 15)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 0, 12)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 0, 20)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 0, 12)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 0, 15)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 0, 20)) | ||
} | ||
|
||
function f2<T extends {} | undefined | null>(a: T, b: T): boolean { | ||
>f2 : Symbol(f2, Decl(unconstrainedTypeComparison.ts, 2, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 4, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 4, 45)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 4, 12)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 4, 50)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 4, 12)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 4, 45)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 4, 50)) | ||
} | ||
|
||
function f3<T extends unknown>(a: T, b: T): boolean { | ||
>f3 : Symbol(f3, Decl(unconstrainedTypeComparison.ts, 6, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 8, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 8, 31)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 8, 12)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 8, 36)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 8, 12)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 8, 31)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 8, 36)) | ||
} | ||
|
||
function f4<T, U extends T>(a: U, b: U): boolean { | ||
>f4 : Symbol(f4, Decl(unconstrainedTypeComparison.ts, 10, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 12, 12)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 12, 14)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 12, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 12, 28)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 12, 14)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 12, 33)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 12, 14)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 12, 28)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 12, 33)) | ||
} | ||
|
||
function f5<T extends {} | undefined | null, U extends T>(a: U, b: U): boolean { | ||
>f5 : Symbol(f5, Decl(unconstrainedTypeComparison.ts, 14, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 16, 12)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 16, 44)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 16, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 16, 58)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 16, 44)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 16, 63)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 16, 44)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 16, 58)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 16, 63)) | ||
} | ||
|
||
function f6<T extends unknown, U extends T>(a: U, b: U): boolean { | ||
>f6 : Symbol(f6, Decl(unconstrainedTypeComparison.ts, 18, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 20, 12)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 20, 30)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 20, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 20, 44)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 20, 30)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 20, 49)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 20, 30)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 20, 44)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 20, 49)) | ||
} | ||
|
||
function f7<T extends {} | undefined, U extends T>(a: U, b: U): boolean { | ||
>f7 : Symbol(f7, Decl(unconstrainedTypeComparison.ts, 22, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 24, 12)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 24, 37)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 24, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 24, 51)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 24, 37)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 24, 56)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 24, 37)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 24, 51)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 24, 56)) | ||
} | ||
|
||
function f8<T extends {} | null, U extends T>(a: U, b: U): boolean { | ||
>f8 : Symbol(f8, Decl(unconstrainedTypeComparison.ts, 26, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 28, 12)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 28, 32)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 28, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 28, 46)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 28, 32)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 28, 51)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 28, 32)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 28, 46)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 28, 51)) | ||
} | ||
|
||
function f9<T extends undefined | null, U extends T>(a: U, b: U): boolean { | ||
>f9 : Symbol(f9, Decl(unconstrainedTypeComparison.ts, 30, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 32, 12)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 32, 39)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 32, 12)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 32, 53)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 32, 39)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 32, 58)) | ||
>U : Symbol(U, Decl(unconstrainedTypeComparison.ts, 32, 39)) | ||
|
||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 32, 53)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 32, 58)) | ||
} | ||
|
||
|
||
function compare<T>(a: T, b: T): boolean { | ||
>compare : Symbol(compare, Decl(unconstrainedTypeComparison.ts, 34, 1)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 37, 17)) | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 37, 20)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 37, 17)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 37, 25)) | ||
>T : Symbol(T, Decl(unconstrainedTypeComparison.ts, 37, 17)) | ||
|
||
if (a === undefined) { | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 37, 20)) | ||
>undefined : Symbol(undefined) | ||
|
||
return false; | ||
} | ||
if (b === null) { | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 37, 25)) | ||
|
||
return false; | ||
} | ||
return a > b; | ||
>a : Symbol(a, Decl(unconstrainedTypeComparison.ts, 37, 20)) | ||
>b : Symbol(b, Decl(unconstrainedTypeComparison.ts, 37, 25)) | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming from #59059 that making this change to
getTypeFacts
breaks a lot of existing code, is that right?Also, does making this change to
checkNonNullType
instead also break a lot of code?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried that and putting this change directly into
checkNonNullType
gets a very similar result to #59059 in terms of breaking object index checkingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of the cases that break relate to this type of index access #59059 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this it's noticeably different in semantics, but istead of doing this, you might want to try to modify
getBaseTypeOfLiteralTypeForComparison
and rename itgetBaseTypeForComparison
. Then pass the result of that intocheckNonNullType
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also consider renaming this to
getBaseConstraintOrUnknown
, use??
instead of||
, and just get rid of thestrictNullChecks
check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also - any clue if this works on unions of unconstrained type parameters? Can you add a test?