Skip to content
Permalink
Browse files
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 <293473+sandersn@users.noreply.github.com>
  • Loading branch information
norech and sandersn committed Sep 15, 2022
1 parent a3f51b3 commit 8b35c1300e14ebc026b4f1621db8f6f1bba30833
Show file tree
Hide file tree
Showing 71 changed files with 1,793 additions and 1,622 deletions.
@@ -29172,11 +29172,30 @@ namespace ts {
}

function reportObjectPossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
Diagnostics.Object_is_possibly_null_or_undefined :
Diagnostics.Object_is_possibly_undefined :
Diagnostics.Object_is_possibly_null
);
const nodeText = isEntityNameExpression(node) ? entityNameToString(node) : undefined;
if (node.kind === SyntaxKind.NullKeyword) {
error(node, Diagnostics.The_value_0_cannot_be_used_here, "null");
return;
}
if (nodeText !== undefined && nodeText.length < 100) {
if (isIdentifier(node) && nodeText === "undefined") {
error(node, Diagnostics.The_value_0_cannot_be_used_here, "undefined");
return;
}
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
Diagnostics._0_is_possibly_null_or_undefined :
Diagnostics._0_is_possibly_undefined :
Diagnostics._0_is_possibly_null,
nodeText
);
}
else {
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
Diagnostics.Object_is_possibly_null_or_undefined :
Diagnostics.Object_is_possibly_undefined :
Diagnostics.Object_is_possibly_null
);
}
}

function reportCannotInvokePossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
@@ -29193,6 +29212,13 @@ namespace ts {
reportError: (node: Node, facts: TypeFacts) => void
): Type {
if (strictNullChecks && type.flags & TypeFlags.Unknown) {
if (isEntityNameExpression(node)) {
const nodeText = entityNameToString(node);
if (nodeText.length < 100) {
error(node, Diagnostics._0_is_of_type_unknown, nodeText);
return errorType;
}
}
error(node, Diagnostics.Object_is_of_type_unknown);
return errorType;
}
@@ -29212,6 +29238,17 @@ namespace ts {
function checkNonNullNonVoidType(type: Type, node: Node): Type {
const nonNullType = checkNonNullType(type, node);
if (nonNullType.flags & TypeFlags.Void) {
if (isEntityNameExpression(node)) {
const nodeText = entityNameToString(node);
if (isIdentifier(node) && nodeText === "undefined") {
error(node, Diagnostics.The_value_0_cannot_be_used_here, nodeText);
return nonNullType;
}
if (nodeText.length < 100) {
error(node, Diagnostics._0_is_possibly_undefined, nodeText);
return nonNullType;
}
}
error(node, Diagnostics.Object_is_possibly_undefined);
}
return nonNullType;
@@ -7497,5 +7497,25 @@
"Properties with the 'accessor' modifier are only available when targeting ECMAScript 2015 and higher.": {
"category": "Error",
"code": 18045
},
"'{0}' is of type 'unknown'.": {
"category": "Error",
"code": 18046
},
"'{0}' is possibly 'null'.": {
"category": "Error",
"code": 18047
},
"'{0}' is possibly 'undefined'.": {
"category": "Error",
"code": 18048
},
"'{0}' is possibly 'null' or 'undefined'.": {
"category": "Error",
"code": 18049
},
"The value '{0}' cannot be used here.": {
"category": "Error",
"code": 18050
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -1,7 +1,7 @@
tests/cases/compiler/binaryArithmatic1.ts(1,13): error TS2531: Object is possibly 'null'.
tests/cases/compiler/binaryArithmatic1.ts(1,13): error TS18050: The value 'null' cannot be used here.


==== tests/cases/compiler/binaryArithmatic1.ts (1 errors) ====
var v = 4 | null;
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
@@ -1,7 +1,7 @@
tests/cases/compiler/binaryArithmatic2.ts(1,13): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/binaryArithmatic2.ts(1,13): error TS18050: The value 'undefined' cannot be used here.


==== tests/cases/compiler/binaryArithmatic2.ts (1 errors) ====
var v = 4 | undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
@@ -1,10 +1,10 @@
tests/cases/compiler/binaryArithmatic3.ts(1,9): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/binaryArithmatic3.ts(1,21): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/binaryArithmatic3.ts(1,9): error TS18050: The value 'undefined' cannot be used here.
tests/cases/compiler/binaryArithmatic3.ts(1,21): error TS18050: The value 'undefined' cannot be used here.


==== tests/cases/compiler/binaryArithmatic3.ts (2 errors) ====
var v = undefined | undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
@@ -1,10 +1,10 @@
tests/cases/compiler/binaryArithmatic4.ts(1,9): error TS2531: Object is possibly 'null'.
tests/cases/compiler/binaryArithmatic4.ts(1,16): error TS2531: Object is possibly 'null'.
tests/cases/compiler/binaryArithmatic4.ts(1,9): error TS18050: The value 'null' cannot be used here.
tests/cases/compiler/binaryArithmatic4.ts(1,16): error TS18050: The value 'null' cannot be used here.


==== tests/cases/compiler/binaryArithmatic4.ts (2 errors) ====
var v = null | null;
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
@@ -1,5 +1,5 @@
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(34,24): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(35,24): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(34,24): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(35,24): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(46,26): error TS2365: Operator '+' cannot be applied to types 'null' and 'undefined'.
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(47,26): error TS2365: Operator '+' cannot be applied to types 'null' and 'null'.
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(48,26): error TS2365: Operator '+' cannot be applied to types 'undefined' and 'undefined'.
@@ -41,10 +41,10 @@ tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNot
// any type literal
var ResultIsNumber6 = ~undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
var ResultIsNumber7 = ~null;
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.

// any type expressions
var ResultIsNumber8 = ~ANY2[0]
@@ -1,6 +1,6 @@
tests/cases/compiler/circularOptionalityRemoval.ts(2,14): error TS2502: 'x' is referenced directly or indirectly in its own type annotation.
tests/cases/compiler/circularOptionalityRemoval.ts(2,38): error TS2372: Parameter 'x' cannot reference itself.
tests/cases/compiler/circularOptionalityRemoval.ts(2,38): error TS2532: Object is possibly 'undefined'.
tests/cases/compiler/circularOptionalityRemoval.ts(2,38): error TS18048: 'x' is possibly 'undefined'.
tests/cases/compiler/circularOptionalityRemoval.ts(2,46): error TS2372: Parameter 'x' cannot reference itself.
tests/cases/compiler/circularOptionalityRemoval.ts(5,14): error TS1015: Parameter cannot have question mark and initializer.
tests/cases/compiler/circularOptionalityRemoval.ts(5,14): error TS2502: 'x' is referenced directly or indirectly in its own type annotation.
@@ -16,7 +16,7 @@ tests/cases/compiler/circularOptionalityRemoval.ts(5,54): error TS2372: Paramete
~
!!! error TS2372: Parameter 'x' cannot reference itself.
~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18048: 'x' is possibly 'undefined'.
~
!!! error TS2372: Parameter 'x' cannot reference itself.

@@ -1,19 +1,19 @@
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(15,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(15,18): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(16,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(16,23): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(24,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(24,18): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(25,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(25,23): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(33,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(33,19): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(34,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(34,24): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(42,11): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(42,19): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(43,11): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(43,24): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(15,11): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(15,18): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(16,11): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(16,23): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(24,11): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(24,18): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(25,11): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(25,23): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(33,11): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(33,19): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(34,11): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(34,24): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(42,11): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(42,19): error TS18050: The value 'null' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(43,11): error TS18050: The value 'undefined' cannot be used here.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(43,24): error TS18050: The value 'undefined' cannot be used here.


==== tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts (16 errors) ====
@@ -33,14 +33,14 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
var ra5 = e < e;
var ra6 = null < null;
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
var ra7 = undefined < undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.

// operator >
var rb1 = a > a;
@@ -50,14 +50,14 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
var rb5 = e > e;
var rb6 = null > null;
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
var rb7 = undefined > undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.

// operator <=
var rc1 = a <= a;
@@ -67,14 +67,14 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
var rc5 = e <= e;
var rc6 = null <= null;
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
var rc7 = undefined <= undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.

// operator >=
var rd1 = a >= a;
@@ -84,14 +84,14 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
var rd5 = e >= e;
var rd6 = null >= null;
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
~~~~
!!! error TS2531: Object is possibly 'null'.
!!! error TS18050: The value 'null' cannot be used here.
var rd7 = undefined >= undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18050: The value 'undefined' cannot be used here.

// operator ==
var re1 = a == a;

0 comments on commit 8b35c13

Please sign in to comment.