Skip to content

Commit f75a1dc

Browse files
authored
Merge pull request #19726 from Microsoft/fixNeverTypeCall
Disallow calls on never type
2 parents d998e97 + fc40a3f commit f75a1dc

11 files changed

+45
-29
lines changed

src/compiler/checker.ts

+3-15
Original file line numberDiff line numberDiff line change
@@ -16630,21 +16630,9 @@ namespace ts {
1663016630
* but is a subtype of the Function interface, the call is an untyped function call.
1663116631
*/
1663216632
function isUntypedFunctionCall(funcType: Type, apparentFuncType: Type, numCallSignatures: number, numConstructSignatures: number) {
16633-
if (isTypeAny(funcType)) {
16634-
return true;
16635-
}
16636-
if (isTypeAny(apparentFuncType) && funcType.flags & TypeFlags.TypeParameter) {
16637-
return true;
16638-
}
16639-
if (!numCallSignatures && !numConstructSignatures) {
16640-
// We exclude union types because we may have a union of function types that happen to have
16641-
// no common signatures.
16642-
if (funcType.flags & TypeFlags.Union) {
16643-
return false;
16644-
}
16645-
return isTypeAssignableTo(funcType, globalFunctionType);
16646-
}
16647-
return false;
16633+
// We exclude union types because we may have a union of function types that happen to have no common signatures.
16634+
return isTypeAny(funcType) || isTypeAny(apparentFuncType) && funcType.flags & TypeFlags.TypeParameter ||
16635+
!numCallSignatures && !numConstructSignatures && !(funcType.flags & (TypeFlags.Union | TypeFlags.Never)) && isTypeAssignableTo(funcType, globalFunctionType);
1664816636
}
1664916637

1665016638
function resolveNewExpression(node: NewExpression, candidatesOutArray: Signature[]): Signature {

tests/baselines/reference/neverTypeErrors1.errors.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(5,5): error TS2322: Type
44
tests/cases/conformance/types/never/neverTypeErrors1.ts(6,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
55
tests/cases/conformance/types/never/neverTypeErrors1.ts(7,5): error TS2322: Type 'null' is not assignable to type 'never'.
66
tests/cases/conformance/types/never/neverTypeErrors1.ts(8,5): error TS2322: Type '{}' is not assignable to type 'never'.
7-
tests/cases/conformance/types/never/neverTypeErrors1.ts(12,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
8-
tests/cases/conformance/types/never/neverTypeErrors1.ts(16,5): error TS2322: Type '1' is not assignable to type 'never'.
9-
tests/cases/conformance/types/never/neverTypeErrors1.ts(19,16): error TS2534: A function returning 'never' cannot have a reachable end point.
7+
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.
8+
tests/cases/conformance/types/never/neverTypeErrors1.ts(13,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
9+
tests/cases/conformance/types/never/neverTypeErrors1.ts(17,5): error TS2322: Type '1' is not assignable to type 'never'.
10+
tests/cases/conformance/types/never/neverTypeErrors1.ts(20,16): error TS2534: A function returning 'never' cannot have a reachable end point.
1011

1112

12-
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (9 errors) ====
13+
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (10 errors) ====
1314
function f1() {
1415
let x: never;
1516
x = 1;
@@ -30,6 +31,9 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(19,16): error TS2534: A
3031
x = {};
3132
~
3233
!!! error TS2322: Type '{}' is not assignable to type 'never'.
34+
x();
35+
~~~
36+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'never' has no compatible call signatures.
3337
}
3438

3539
function f2(): never {

tests/baselines/reference/neverTypeErrors1.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function f1() {
77
x = undefined;
88
x = null;
99
x = {};
10+
x();
1011
}
1112

1213
function f2(): never {
@@ -29,6 +30,7 @@ function f1() {
2930
x = undefined;
3031
x = null;
3132
x = {};
33+
x();
3234
}
3335
function f2() {
3436
return;

tests/baselines/reference/neverTypeErrors1.symbols

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ function f1() {
2323

2424
x = {};
2525
>x : Symbol(x, Decl(neverTypeErrors1.ts, 1, 7))
26+
27+
x();
28+
>x : Symbol(x, Decl(neverTypeErrors1.ts, 1, 7))
2629
}
2730

2831
function f2(): never {
29-
>f2 : Symbol(f2, Decl(neverTypeErrors1.ts, 8, 1))
32+
>f2 : Symbol(f2, Decl(neverTypeErrors1.ts, 9, 1))
3033

3134
return;
3235
}
3336

3437
function f3(): never {
35-
>f3 : Symbol(f3, Decl(neverTypeErrors1.ts, 12, 1))
38+
>f3 : Symbol(f3, Decl(neverTypeErrors1.ts, 13, 1))
3639

3740
return 1;
3841
}
3942

4043
function f4(): never {
41-
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 16, 1))
44+
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 17, 1))
4245
}

tests/baselines/reference/neverTypeErrors1.types

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function f1() {
3434
>x = {} : {}
3535
>x : never
3636
>{} : {}
37+
38+
x();
39+
>x() : any
40+
>x : never
3741
}
3842

3943
function f2(): never {

tests/baselines/reference/neverTypeErrors2.errors.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ tests/cases/conformance/types/never/neverTypeErrors2.ts(5,5): error TS2322: Type
44
tests/cases/conformance/types/never/neverTypeErrors2.ts(6,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
55
tests/cases/conformance/types/never/neverTypeErrors2.ts(7,5): error TS2322: Type 'null' is not assignable to type 'never'.
66
tests/cases/conformance/types/never/neverTypeErrors2.ts(8,5): error TS2322: Type '{}' is not assignable to type 'never'.
7-
tests/cases/conformance/types/never/neverTypeErrors2.ts(12,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
8-
tests/cases/conformance/types/never/neverTypeErrors2.ts(16,5): error TS2322: Type '1' is not assignable to type 'never'.
9-
tests/cases/conformance/types/never/neverTypeErrors2.ts(19,16): error TS2534: A function returning 'never' cannot have a reachable end point.
7+
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.
8+
tests/cases/conformance/types/never/neverTypeErrors2.ts(13,5): error TS2322: Type 'undefined' is not assignable to type 'never'.
9+
tests/cases/conformance/types/never/neverTypeErrors2.ts(17,5): error TS2322: Type '1' is not assignable to type 'never'.
10+
tests/cases/conformance/types/never/neverTypeErrors2.ts(20,16): error TS2534: A function returning 'never' cannot have a reachable end point.
1011

1112

12-
==== tests/cases/conformance/types/never/neverTypeErrors2.ts (9 errors) ====
13+
==== tests/cases/conformance/types/never/neverTypeErrors2.ts (10 errors) ====
1314
function f1() {
1415
let x: never;
1516
x = 1;
@@ -30,6 +31,9 @@ tests/cases/conformance/types/never/neverTypeErrors2.ts(19,16): error TS2534: A
3031
x = {};
3132
~
3233
!!! error TS2322: Type '{}' is not assignable to type 'never'.
34+
x();
35+
~~~
36+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'never' has no compatible call signatures.
3337
}
3438

3539
function f2(): never {

tests/baselines/reference/neverTypeErrors2.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function f1() {
77
x = undefined;
88
x = null;
99
x = {};
10+
x();
1011
}
1112

1213
function f2(): never {
@@ -29,6 +30,7 @@ function f1() {
2930
x = undefined;
3031
x = null;
3132
x = {};
33+
x();
3234
}
3335
function f2() {
3436
return;

tests/baselines/reference/neverTypeErrors2.symbols

+6-3
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ function f1() {
2323

2424
x = {};
2525
>x : Symbol(x, Decl(neverTypeErrors2.ts, 1, 7))
26+
27+
x();
28+
>x : Symbol(x, Decl(neverTypeErrors2.ts, 1, 7))
2629
}
2730

2831
function f2(): never {
29-
>f2 : Symbol(f2, Decl(neverTypeErrors2.ts, 8, 1))
32+
>f2 : Symbol(f2, Decl(neverTypeErrors2.ts, 9, 1))
3033

3134
return;
3235
}
3336

3437
function f3(): never {
35-
>f3 : Symbol(f3, Decl(neverTypeErrors2.ts, 12, 1))
38+
>f3 : Symbol(f3, Decl(neverTypeErrors2.ts, 13, 1))
3639

3740
return 1;
3841
}
3942

4043
function f4(): never {
41-
>f4 : Symbol(f4, Decl(neverTypeErrors2.ts, 16, 1))
44+
>f4 : Symbol(f4, Decl(neverTypeErrors2.ts, 17, 1))
4245
}

tests/baselines/reference/neverTypeErrors2.types

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function f1() {
3434
>x = {} : {}
3535
>x : never
3636
>{} : {}
37+
38+
x();
39+
>x() : any
40+
>x : never
3741
}
3842

3943
function f2(): never {

tests/cases/conformance/types/never/neverTypeErrors1.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ function f1() {
66
x = undefined;
77
x = null;
88
x = {};
9+
x();
910
}
1011

1112
function f2(): never {

tests/cases/conformance/types/never/neverTypeErrors2.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function f1() {
88
x = undefined;
99
x = null;
1010
x = {};
11+
x();
1112
}
1213

1314
function f2(): never {

0 commit comments

Comments
 (0)