Skip to content
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

The error "Object is possibly null or undefined" is ambiguous. #49797

Merged
merged 14 commits into from Sep 15, 2022
Merged
26 changes: 26 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -28948,6 +28948,18 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
}

function reportObjectPossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
if (isEntityNameExpression(node)) {
const nodeText = entityNameToString(node);
norech marked this conversation as resolved.
Show resolved Hide resolved
if (nodeText.length < 100) {
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
);
return;
}
}
error(node, facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
Diagnostics.Object_is_possibly_null_or_undefined :
Diagnostics.Object_is_possibly_undefined :
Expand All @@ -28969,6 +28981,13 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
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;
}
Expand All @@ -28988,6 +29007,13 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
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 (nodeText.length < 100) {
error(node, Diagnostics._0_is_possibly_undefined, nodeText);
return nonNullType;
}
}
error(node, Diagnostics.Object_is_possibly_undefined);
}
return nonNullType;
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -7421,5 +7421,21 @@
"'{0}' is automatically exported here.": {
"category": "Message",
"code": 18044
},
"'{0}' is of type 'unknown'.": {
"category": "Error",
"code": 18045
},
"'{0}' is possibly 'null'.": {
"category": "Error",
"code": 18046
},
"'{0}' is possibly 'undefined'.": {
"category": "Error",
"code": 18047
},
"'{0}' is possibly 'null' or 'undefined'.": {
"category": "Error",
"code": 18048
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/baselines/reference/binaryArithmatic2.errors.txt
@@ -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 TS18047: 'undefined' is possibly 'undefined'.
norech marked this conversation as resolved.
Show resolved Hide resolved


==== tests/cases/compiler/binaryArithmatic2.ts (1 errors) ====
var v = 4 | undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
8 changes: 4 additions & 4 deletions tests/baselines/reference/binaryArithmatic3.errors.txt
@@ -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 TS18047: 'undefined' is possibly 'undefined'.
tests/cases/compiler/binaryArithmatic3.ts(1,21): error TS18047: 'undefined' is possibly 'undefined'.


==== tests/cases/compiler/binaryArithmatic3.ts (2 errors) ====
var v = undefined | undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
@@ -1,4 +1,4 @@
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(34,24): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNotOperatorWithAnyOtherType.ts(34,24): error TS18047: 'undefined' 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(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'.
Expand Down Expand Up @@ -41,7 +41,7 @@ tests/cases/conformance/expressions/unaryOperators/bitwiseNotOperator/bitwiseNot
// any type literal
var ResultIsNumber6 = ~undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
var ResultIsNumber7 = ~null;
~~~~
!!! error TS2531: Object is possibly 'null'.
Expand Down
@@ -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 TS18047: '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.
Expand All @@ -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 TS18047: 'x' is possibly 'undefined'.
~
!!! error TS2372: Parameter 'x' cannot reference itself.

Expand Down
@@ -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(16,11): error TS18047: 'undefined' is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(16,23): error TS18047: 'undefined' 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(25,11): error TS18047: 'undefined' is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(25,23): error TS18047: 'undefined' 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(34,11): error TS18047: 'undefined' is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(34,24): error TS18047: 'undefined' 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(43,11): error TS18047: 'undefined' is possibly 'undefined'.
tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts(43,24): error TS18047: 'undefined' is possibly 'undefined'.


==== tests/cases/conformance/expressions/binaryOperators/comparisonOperator/comparisonOperatorWithIdenticalPrimitiveType.ts (16 errors) ====
Expand All @@ -38,9 +38,9 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
!!! error TS2531: Object is possibly 'null'.
var ra7 = undefined < undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.

// operator >
var rb1 = a > a;
Expand All @@ -55,9 +55,9 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
!!! error TS2531: Object is possibly 'null'.
var rb7 = undefined > undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.

// operator <=
var rc1 = a <= a;
Expand All @@ -72,9 +72,9 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
!!! error TS2531: Object is possibly 'null'.
var rc7 = undefined <= undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.

// operator >=
var rd1 = a >= a;
Expand All @@ -89,9 +89,9 @@ tests/cases/conformance/expressions/binaryOperators/comparisonOperator/compariso
!!! error TS2531: Object is possibly 'null'.
var rd7 = undefined >= undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.

// operator ==
var re1 = a == a;
Expand Down
@@ -1,9 +1,9 @@
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(11,7): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(12,7): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(12,7): error TS18047: 'undefined' is possibly 'undefined'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(18,7): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(19,7): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(19,7): error TS18047: 'undefined' is possibly 'undefined'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(25,7): error TS2531: Object is possibly 'null'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(26,7): error TS2532: Object is possibly 'undefined'.
tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts(26,7): error TS18047: 'undefined' is possibly 'undefined'.


==== tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignmentLHSCanBeAssigned.ts (6 errors) ====
Expand All @@ -22,7 +22,7 @@ tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignm
!!! error TS2531: Object is possibly 'null'.
x1 *= undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.

var x2: number;
x2 *= a;
Expand All @@ -33,7 +33,7 @@ tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignm
!!! error TS2531: Object is possibly 'null'.
x2 *= undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.

var x3: E;
x3 *= a;
Expand All @@ -44,4 +44,4 @@ tests/cases/conformance/expressions/assignmentOperator/compoundArithmeticAssignm
!!! error TS2531: Object is possibly 'null'.
x3 *= undefined;
~~~~~~~~~
!!! error TS2532: Object is possibly 'undefined'.
!!! error TS18047: 'undefined' is possibly 'undefined'.