* Added test case for #20026 * Implemented #20026 * Addresed comments at Microsoft/TypeScript/pull/20157#discussion_r152086287 * Fixed merge issues * Fixed baseline issue * Merged upstream
This commit is contained in:
parent
c4d76292f1
commit
73e3e8d790
7 changed files with 107 additions and 9 deletions
|
@ -15845,17 +15845,35 @@ namespace ts {
|
|||
});
|
||||
}
|
||||
|
||||
function checkNonNullExpression(node: Expression | QualifiedName) {
|
||||
return checkNonNullType(checkExpression(node), node);
|
||||
function checkNonNullExpression(
|
||||
node: Expression | QualifiedName,
|
||||
nullDiagnostic?: DiagnosticMessage,
|
||||
undefinedDiagnostic?: DiagnosticMessage,
|
||||
nullOrUndefinedDiagnostic?: DiagnosticMessage,
|
||||
) {
|
||||
return checkNonNullType(
|
||||
checkExpression(node),
|
||||
node,
|
||||
nullDiagnostic,
|
||||
undefinedDiagnostic,
|
||||
nullOrUndefinedDiagnostic
|
||||
);
|
||||
}
|
||||
|
||||
function checkNonNullType(type: Type, errorNode: Node): Type {
|
||||
function checkNonNullType(
|
||||
type: Type,
|
||||
node: Node,
|
||||
nullDiagnostic?: DiagnosticMessage,
|
||||
undefinedDiagnostic?: DiagnosticMessage,
|
||||
nullOrUndefinedDiagnostic?: DiagnosticMessage
|
||||
): Type {
|
||||
const kind = (strictNullChecks ? getFalsyFlags(type) : type.flags) & TypeFlags.Nullable;
|
||||
if (kind) {
|
||||
error(errorNode, kind & TypeFlags.Undefined ? kind & TypeFlags.Null ?
|
||||
Diagnostics.Object_is_possibly_null_or_undefined :
|
||||
Diagnostics.Object_is_possibly_undefined :
|
||||
Diagnostics.Object_is_possibly_null);
|
||||
error(node, kind & TypeFlags.Undefined ? kind & TypeFlags.Null ?
|
||||
(nullOrUndefinedDiagnostic || Diagnostics.Object_is_possibly_null_or_undefined) :
|
||||
(undefinedDiagnostic || Diagnostics.Object_is_possibly_undefined) :
|
||||
(nullDiagnostic || Diagnostics.Object_is_possibly_null)
|
||||
);
|
||||
const t = getNonNullableType(type);
|
||||
return t.flags & (TypeFlags.Nullable | TypeFlags.Never) ? unknownType : t;
|
||||
}
|
||||
|
@ -17364,7 +17382,13 @@ namespace ts {
|
|||
return resolveUntypedCall(node);
|
||||
}
|
||||
|
||||
const funcType = checkNonNullExpression(node.expression);
|
||||
const funcType = checkNonNullExpression(
|
||||
node.expression,
|
||||
Diagnostics.Cannot_invoke_an_object_which_is_possibly_null,
|
||||
Diagnostics.Cannot_invoke_an_object_which_is_possibly_undefined,
|
||||
Diagnostics.Cannot_invoke_an_object_which_is_possibly_null_or_undefined
|
||||
);
|
||||
|
||||
if (funcType === silentNeverType) {
|
||||
return silentNeverSignature;
|
||||
}
|
||||
|
|
|
@ -2288,7 +2288,18 @@
|
|||
"category": "Error",
|
||||
"code": 2720
|
||||
},
|
||||
|
||||
"Cannot invoke an object which is possibly 'null'.": {
|
||||
"category": "Error",
|
||||
"code": 2721
|
||||
},
|
||||
"Cannot invoke an object which is possibly 'undefined'.": {
|
||||
"category": "Error",
|
||||
"code": 2722
|
||||
},
|
||||
"Cannot invoke an object which is possibly 'null' or 'undefined'.": {
|
||||
"category": "Error",
|
||||
"code": 2723
|
||||
},
|
||||
"Import declaration '{0}' is using private name '{1}'.": {
|
||||
"category": "Error",
|
||||
"code": 4000
|
||||
|
|
17
tests/baselines/reference/nullableFunctionError.errors.txt
Normal file
17
tests/baselines/reference/nullableFunctionError.errors.txt
Normal file
|
@ -0,0 +1,17 @@
|
|||
tests/cases/compiler/nullableFunctionError.ts(1,1): error TS2721: Cannot invoke an object which is possibly 'null'.
|
||||
tests/cases/compiler/nullableFunctionError.ts(2,1): error TS2722: Cannot invoke an object which is possibly 'undefined'.
|
||||
tests/cases/compiler/nullableFunctionError.ts(4,1): error TS2723: Cannot invoke an object which is possibly 'null' or 'undefined'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/nullableFunctionError.ts (3 errors) ====
|
||||
null();
|
||||
~~~~
|
||||
!!! error TS2721: Cannot invoke an object which is possibly 'null'.
|
||||
undefined();
|
||||
~~~~~~~~~
|
||||
!!! error TS2722: Cannot invoke an object which is possibly 'undefined'.
|
||||
let f: null | undefined;
|
||||
f();
|
||||
~
|
||||
!!! error TS2723: Cannot invoke an object which is possibly 'null' or 'undefined'.
|
||||
|
12
tests/baselines/reference/nullableFunctionError.js
Normal file
12
tests/baselines/reference/nullableFunctionError.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
//// [nullableFunctionError.ts]
|
||||
null();
|
||||
undefined();
|
||||
let f: null | undefined;
|
||||
f();
|
||||
|
||||
|
||||
//// [nullableFunctionError.js]
|
||||
null();
|
||||
undefined();
|
||||
var f;
|
||||
f();
|
11
tests/baselines/reference/nullableFunctionError.symbols
Normal file
11
tests/baselines/reference/nullableFunctionError.symbols
Normal file
|
@ -0,0 +1,11 @@
|
|||
=== tests/cases/compiler/nullableFunctionError.ts ===
|
||||
null();
|
||||
undefined();
|
||||
>undefined : Symbol(undefined)
|
||||
|
||||
let f: null | undefined;
|
||||
>f : Symbol(f, Decl(nullableFunctionError.ts, 2, 3))
|
||||
|
||||
f();
|
||||
>f : Symbol(f, Decl(nullableFunctionError.ts, 2, 3))
|
||||
|
17
tests/baselines/reference/nullableFunctionError.types
Normal file
17
tests/baselines/reference/nullableFunctionError.types
Normal file
|
@ -0,0 +1,17 @@
|
|||
=== tests/cases/compiler/nullableFunctionError.ts ===
|
||||
null();
|
||||
>null() : any
|
||||
>null : null
|
||||
|
||||
undefined();
|
||||
>undefined() : any
|
||||
>undefined : undefined
|
||||
|
||||
let f: null | undefined;
|
||||
>f : null | undefined
|
||||
>null : null
|
||||
|
||||
f();
|
||||
>f() : any
|
||||
>f : null | undefined
|
||||
|
6
tests/cases/compiler/nullableFunctionError.ts
Normal file
6
tests/cases/compiler/nullableFunctionError.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
// @strictNullChecks: true
|
||||
|
||||
null();
|
||||
undefined();
|
||||
let f: null | undefined;
|
||||
f();
|
Loading…
Reference in a new issue