* 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:
Remo H. Jansen 2018-01-09 01:25:56 +00:00 committed by Mohamed Hegazy
parent c4d76292f1
commit 73e3e8d790
7 changed files with 107 additions and 9 deletions

View file

@ -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;
}

View file

@ -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

View 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'.

View file

@ -0,0 +1,12 @@
//// [nullableFunctionError.ts]
null();
undefined();
let f: null | undefined;
f();
//// [nullableFunctionError.js]
null();
undefined();
var f;
f();

View 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))

View 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

View file

@ -0,0 +1,6 @@
// @strictNullChecks: true
null();
undefined();
let f: null | undefined;
f();