Merge pull request #19726 from Microsoft/fixNeverTypeCall

Disallow calls on never type
This commit is contained in:
Anders Hejlsberg
2017-11-03 15:39:19 -07:00
committed by GitHub
11 changed files with 45 additions and 29 deletions
+3 -15
View File
@@ -16630,21 +16630,9 @@ namespace ts {
* but is a subtype of the Function interface, the call is an untyped function call.
*/
function isUntypedFunctionCall(funcType: Type, apparentFuncType: Type, numCallSignatures: number, numConstructSignatures: number) {
if (isTypeAny(funcType)) {
return true;
}
if (isTypeAny(apparentFuncType) && funcType.flags & TypeFlags.TypeParameter) {
return true;
}
if (!numCallSignatures && !numConstructSignatures) {
// We exclude union types because we may have a union of function types that happen to have
// no common signatures.
if (funcType.flags & TypeFlags.Union) {
return false;
}
return isTypeAssignableTo(funcType, globalFunctionType);
}
return false;
// We exclude union types because we may have a union of function types that happen to have no common signatures.
return isTypeAny(funcType) || isTypeAny(apparentFuncType) && funcType.flags & TypeFlags.TypeParameter ||
!numCallSignatures && !numConstructSignatures && !(funcType.flags & (TypeFlags.Union | TypeFlags.Never)) && isTypeAssignableTo(funcType, globalFunctionType);
}
function resolveNewExpression(node: NewExpression, candidatesOutArray: Signature[]): Signature {
@@ -4,12 +4,13 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(5,5): error TS2322: Type
tests/cases/conformance/types/never/neverTypeErrors1.ts(6,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(7,5): error TS2322: Type 'null' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(8,5): error TS2322: Type '{}' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(12,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(16,5): error TS2322: Type '1' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(19,16): error TS2534: A function returning 'never' cannot have a reachable end point.
tests/cases/conformance/types/never/neverTypeErrors1.ts(9,5): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'never' has no compatible call signatures.
tests/cases/conformance/types/never/neverTypeErrors1.ts(13,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(17,5): error TS2322: Type '1' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(20,16): error TS2534: A function returning 'never' cannot have a reachable end point.
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (9 errors) ====
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (10 errors) ====
function f1() {
let x: never;
x = 1;
@@ -30,6 +31,9 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(19,16): error TS2534: A
x = {};
~
!!! error TS2322: Type '{}' is not assignable to type 'never'.
x();
~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'never' has no compatible call signatures.
}
function f2(): never {
@@ -7,6 +7,7 @@ function f1() {
x = undefined;
x = null;
x = {};
x();
}
function f2(): never {
@@ -29,6 +30,7 @@ function f1() {
x = undefined;
x = null;
x = {};
x();
}
function f2() {
return;
@@ -23,20 +23,23 @@ function f1() {
x = {};
>x : Symbol(x, Decl(neverTypeErrors1.ts, 1, 7))
x();
>x : Symbol(x, Decl(neverTypeErrors1.ts, 1, 7))
}
function f2(): never {
>f2 : Symbol(f2, Decl(neverTypeErrors1.ts, 8, 1))
>f2 : Symbol(f2, Decl(neverTypeErrors1.ts, 9, 1))
return;
}
function f3(): never {
>f3 : Symbol(f3, Decl(neverTypeErrors1.ts, 12, 1))
>f3 : Symbol(f3, Decl(neverTypeErrors1.ts, 13, 1))
return 1;
}
function f4(): never {
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 16, 1))
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 17, 1))
}
@@ -34,6 +34,10 @@ function f1() {
>x = {} : {}
>x : never
>{} : {}
x();
>x() : any
>x : never
}
function f2(): never {
@@ -4,12 +4,13 @@ tests/cases/conformance/types/never/neverTypeErrors2.ts(5,5): error TS2322: Type
tests/cases/conformance/types/never/neverTypeErrors2.ts(6,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(7,5): error TS2322: Type 'null' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(8,5): error TS2322: Type '{}' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(12,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(16,5): error TS2322: Type '1' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(19,16): error TS2534: A function returning 'never' cannot have a reachable end point.
tests/cases/conformance/types/never/neverTypeErrors2.ts(9,5): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'never' has no compatible call signatures.
tests/cases/conformance/types/never/neverTypeErrors2.ts(13,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(17,5): error TS2322: Type '1' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(20,16): error TS2534: A function returning 'never' cannot have a reachable end point.
==== tests/cases/conformance/types/never/neverTypeErrors2.ts (9 errors) ====
==== tests/cases/conformance/types/never/neverTypeErrors2.ts (10 errors) ====
function f1() {
let x: never;
x = 1;
@@ -30,6 +31,9 @@ tests/cases/conformance/types/never/neverTypeErrors2.ts(19,16): error TS2534: A
x = {};
~
!!! error TS2322: Type '{}' is not assignable to type 'never'.
x();
~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'never' has no compatible call signatures.
}
function f2(): never {
@@ -7,6 +7,7 @@ function f1() {
x = undefined;
x = null;
x = {};
x();
}
function f2(): never {
@@ -29,6 +30,7 @@ function f1() {
x = undefined;
x = null;
x = {};
x();
}
function f2() {
return;
@@ -23,20 +23,23 @@ function f1() {
x = {};
>x : Symbol(x, Decl(neverTypeErrors2.ts, 1, 7))
x();
>x : Symbol(x, Decl(neverTypeErrors2.ts, 1, 7))
}
function f2(): never {
>f2 : Symbol(f2, Decl(neverTypeErrors2.ts, 8, 1))
>f2 : Symbol(f2, Decl(neverTypeErrors2.ts, 9, 1))
return;
}
function f3(): never {
>f3 : Symbol(f3, Decl(neverTypeErrors2.ts, 12, 1))
>f3 : Symbol(f3, Decl(neverTypeErrors2.ts, 13, 1))
return 1;
}
function f4(): never {
>f4 : Symbol(f4, Decl(neverTypeErrors2.ts, 16, 1))
>f4 : Symbol(f4, Decl(neverTypeErrors2.ts, 17, 1))
}
@@ -34,6 +34,10 @@ function f1() {
>x = {} : {}
>x : never
>{} : {}
x();
>x() : any
>x : never
}
function f2(): never {
@@ -6,6 +6,7 @@ function f1() {
x = undefined;
x = null;
x = {};
x();
}
function f2(): never {
@@ -8,6 +8,7 @@ function f1() {
x = undefined;
x = null;
x = {};
x();
}
function f2(): never {