-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Improved checking of nullable operands in expressions #13483
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bc7f86c
Improved undefined/null handling for arithmetic operators
ahejlsberg a1e16d2
Accept new baselines
ahejlsberg 8ce193c
Improved undefined/null handling for relational operators
ahejlsberg 6dcaac6
Accept new baselines
ahejlsberg 894ba85
Improved undefined/null handling for unary operators
ahejlsberg 221c0d7
Accept new baselines
ahejlsberg 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 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
This file contains 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
63 changes: 63 additions & 0 deletions
63
tests/baselines/reference/additionOperatorWithNullValueAndValidOperator.errors.txt
This file contains 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,63 @@ | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(15,10): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(16,10): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(17,10): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(18,10): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(19,10): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(20,14): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(21,14): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(22,15): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(23,17): error TS2531: Object is possibly 'null'. | ||
tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts(24,20): error TS2531: Object is possibly 'null'. | ||
|
||
|
||
==== tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithNullValueAndValidOperator.ts (10 errors) ==== | ||
// If one operand is the null or undefined value, it is treated as having the type of the other operand. | ||
|
||
enum E { a, b, c } | ||
|
||
var a: any; | ||
var b: number; | ||
var c: E; | ||
var d: string; | ||
|
||
// null + any | ||
var r1: any = null + a; | ||
var r2: any = a + null; | ||
|
||
// null + number/enum | ||
var r3 = null + b; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r4 = null + 1; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r5 = null + c; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r6 = null + E.a; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r7 = null + E['a']; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r8 = b + null; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r9 = 1 + null; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r10 = c + null | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r11 = E.a + null; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
var r12 = E['a'] + null; | ||
~~~~ | ||
!!! error TS2531: Object is possibly 'null'. | ||
|
||
// null + string | ||
var r13 = null + d; | ||
var r14 = null + ''; | ||
var r15 = d + null; | ||
var r16 = '' + null; |
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.
Object is definitely
null
, and typically we don't think of objects as being operands to arithmetic operations (whereas we do when it comes to primitives). Would be nice if we could be more precise on the error message.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.
Two orthogonal issues:
First, we could do extra work to omit the word
possibly
when the type is exactlynull
orundefined
. However, it is very rare for someone to explicitly usenull
orundefined
as an operand, so it's not clear that it really matters all that much. This is the type of error that only surfaces in a test.Second, we could consider using the word
value
instead ofobject
when reporting for expression operands--but it's really the same error that we use forobj.foo
whereobj
may be null or undefined. The downside would be error message searchability, but since the error is pretty obvious that may not be an issue.