Skip to content

Commit 8b35c13

Browse files
norechsandersn
andauthored
The error "Object is possibly null or undefined" is ambiguous. (#49797)
* added object name to TS2571, 2531, 2532 and 2533 * updated localized diagnostic messages * updated baseline to fit diagnostic message change * Revert "updated localized diagnostic messages" This reverts commit 738cf09. * specialized the error to EntityNameExpression * updated baseline to fit new changes * added multiline undefined access test * added TS18049 - value cannot be used here * adjusted baseline * corrected a small linting issue * Update error numbers after merge from main Co-authored-by: Nathan Shively-Sanders <[email protected]>
1 parent a3f51b3 commit 8b35c13

File tree

71 files changed

+1793
-1622
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1793
-1622
lines changed

src/compiler/checker.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29172,11 +29172,30 @@ namespace ts {
2917229172
}
2917329173

2917429174
function reportObjectPossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
29175-
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
29176-
Diagnostics.Object_is_possibly_null_or_undefined :
29177-
Diagnostics.Object_is_possibly_undefined :
29178-
Diagnostics.Object_is_possibly_null
29179-
);
29175+
const nodeText = isEntityNameExpression(node) ? entityNameToString(node) : undefined;
29176+
if (node.kind === SyntaxKind.NullKeyword) {
29177+
error(node, Diagnostics.The_value_0_cannot_be_used_here, "null");
29178+
return;
29179+
}
29180+
if (nodeText !== undefined && nodeText.length < 100) {
29181+
if (isIdentifier(node) && nodeText === "undefined") {
29182+
error(node, Diagnostics.The_value_0_cannot_be_used_here, "undefined");
29183+
return;
29184+
}
29185+
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
29186+
Diagnostics._0_is_possibly_null_or_undefined :
29187+
Diagnostics._0_is_possibly_undefined :
29188+
Diagnostics._0_is_possibly_null,
29189+
nodeText
29190+
);
29191+
}
29192+
else {
29193+
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
29194+
Diagnostics.Object_is_possibly_null_or_undefined :
29195+
Diagnostics.Object_is_possibly_undefined :
29196+
Diagnostics.Object_is_possibly_null
29197+
);
29198+
}
2918029199
}
2918129200

2918229201
function reportCannotInvokePossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
@@ -29193,6 +29212,13 @@ namespace ts {
2919329212
reportError: (node: Node, facts: TypeFacts) => void
2919429213
): Type {
2919529214
if (strictNullChecks && type.flags & TypeFlags.Unknown) {
29215+
if (isEntityNameExpression(node)) {
29216+
const nodeText = entityNameToString(node);
29217+
if (nodeText.length < 100) {
29218+
error(node, Diagnostics._0_is_of_type_unknown, nodeText);
29219+
return errorType;
29220+
}
29221+
}
2919629222
error(node, Diagnostics.Object_is_of_type_unknown);
2919729223
return errorType;
2919829224
}
@@ -29212,6 +29238,17 @@ namespace ts {
2921229238
function checkNonNullNonVoidType(type: Type, node: Node): Type {
2921329239
const nonNullType = checkNonNullType(type, node);
2921429240
if (nonNullType.flags & TypeFlags.Void) {
29241+
if (isEntityNameExpression(node)) {
29242+
const nodeText = entityNameToString(node);
29243+
if (isIdentifier(node) && nodeText === "undefined") {
29244+
error(node, Diagnostics.The_value_0_cannot_be_used_here, nodeText);
29245+
return nonNullType;
29246+
}
29247+
if (nodeText.length < 100) {
29248+
error(node, Diagnostics._0_is_possibly_undefined, nodeText);
29249+
return nonNullType;
29250+
}
29251+
}
2921529252
error(node, Diagnostics.Object_is_possibly_undefined);
2921629253
}
2921729254
return nonNullType;

src/compiler/diagnosticMessages.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7497,5 +7497,25 @@
74977497
"Properties with the 'accessor' modifier are only available when targeting ECMAScript 2015 and higher.": {
74987498
"category": "Error",
74997499
"code": 18045
7500+
},
7501+
"'{0}' is of type 'unknown'.": {
7502+
"category": "Error",
7503+
"code": 18046
7504+
},
7505+
"'{0}' is possibly 'null'.": {
7506+
"category": "Error",
7507+
"code": 18047
7508+
},
7509+
"'{0}' is possibly 'undefined'.": {
7510+
"category": "Error",
7511+
"code": 18048
7512+
},
7513+
"'{0}' is possibly 'null' or 'undefined'.": {
7514+
"category": "Error",
7515+
"code": 18049
7516+
},
7517+
"The value '{0}' cannot be used here.": {
7518+
"category": "Error",
7519+
"code": 18050
75007520
}
75017521
}

tests/baselines/reference/arithmeticOperatorWithNullValueAndInvalidOperands.errors.txt

Lines changed: 240 additions & 240 deletions
Large diffs are not rendered by default.

tests/baselines/reference/arithmeticOperatorWithNullValueAndValidOperands.errors.txt

Lines changed: 160 additions & 160 deletions
Large diffs are not rendered by default.

tests/baselines/reference/arithmeticOperatorWithOnlyNullValueOrUndefinedValue.errors.txt

Lines changed: 160 additions & 160 deletions
Large diffs are not rendered by default.

tests/baselines/reference/arithmeticOperatorWithUndefinedValueAndInvalidOperands.errors.txt

Lines changed: 240 additions & 240 deletions
Large diffs are not rendered by default.

tests/baselines/reference/arithmeticOperatorWithUndefinedValueAndValidOperands.errors.txt

Lines changed: 160 additions & 160 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/compiler/binaryArithmatic1.ts(1,13): error TS2531: Object is possibly 'null'.
1+
tests/cases/compiler/binaryArithmatic1.ts(1,13): error TS18050: The value 'null' cannot be used here.
22

33

44
==== tests/cases/compiler/binaryArithmatic1.ts (1 errors) ====
55
var v = 4 | null;
66
~~~~
7-
!!! error TS2531: Object is possibly 'null'.
7+
!!! error TS18050: The value 'null' cannot be used here.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
tests/cases/compiler/binaryArithmatic2.ts(1,13): error TS2532: Object is possibly 'undefined'.
1+
tests/cases/compiler/binaryArithmatic2.ts(1,13): error TS18050: The value 'undefined' cannot be used here.
22

33

44
==== tests/cases/compiler/binaryArithmatic2.ts (1 errors) ====
55
var v = 4 | undefined;
66
~~~~~~~~~
7-
!!! error TS2532: Object is possibly 'undefined'.
7+
!!! error TS18050: The value 'undefined' cannot be used here.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/binaryArithmatic3.ts(1,9): error TS2532: Object is possibly 'undefined'.
2-
tests/cases/compiler/binaryArithmatic3.ts(1,21): error TS2532: Object is possibly 'undefined'.
1+
tests/cases/compiler/binaryArithmatic3.ts(1,9): error TS18050: The value 'undefined' cannot be used here.
2+
tests/cases/compiler/binaryArithmatic3.ts(1,21): error TS18050: The value 'undefined' cannot be used here.
33

44

55
==== tests/cases/compiler/binaryArithmatic3.ts (2 errors) ====
66
var v = undefined | undefined;
77
~~~~~~~~~
8-
!!! error TS2532: Object is possibly 'undefined'.
8+
!!! error TS18050: The value 'undefined' cannot be used here.
99
~~~~~~~~~
10-
!!! error TS2532: Object is possibly 'undefined'.
10+
!!! error TS18050: The value 'undefined' cannot be used here.

0 commit comments

Comments
 (0)