Skip to content

Commit f6b1a1d

Browse files
authored
Merge pull request microsoft#20404 from Microsoft/fixUntypedFunctionCall
Fix untyped function call on constrained type variable
2 parents 65af685 + 35188c5 commit f6b1a1d

6 files changed

Lines changed: 219 additions & 1 deletion

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17115,7 +17115,7 @@ namespace ts {
1711517115
function isUntypedFunctionCall(funcType: Type, apparentFuncType: Type, numCallSignatures: number, numConstructSignatures: number) {
1711617116
// We exclude union types because we may have a union of function types that happen to have no common signatures.
1711717117
return isTypeAny(funcType) || isTypeAny(apparentFuncType) && funcType.flags & TypeFlags.TypeParameter ||
17118-
!numCallSignatures && !numConstructSignatures && !(funcType.flags & (TypeFlags.Union | TypeFlags.Never)) && isTypeAssignableTo(funcType, globalFunctionType);
17118+
!numCallSignatures && !numConstructSignatures && !(apparentFuncType.flags & (TypeFlags.Union | TypeFlags.Never)) && isTypeAssignableTo(funcType, globalFunctionType);
1711917119
}
1712017120

1712117121
function resolveNewExpression(node: NewExpression, candidatesOutArray: Signature[]): Signature {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts(11,3): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
2+
tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts(15,3): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
3+
tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts(18,3): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
4+
tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts(19,3): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
5+
6+
7+
==== tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts (4 errors) ====
8+
// Repro from #20196
9+
10+
type A = {
11+
a: (x: number) => string
12+
};
13+
type B = {
14+
a: (x: boolean) => string
15+
};
16+
17+
function call0(p: A | B) {
18+
p.a("s"); // Error
19+
~~~~~~~~
20+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
21+
}
22+
23+
function callN<T extends A | B>(p: T) {
24+
p.a("s"); // Error
25+
~~~~~~~~
26+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
27+
28+
var a: T["a"] = p.a;
29+
a(""); // Error
30+
~~~~~
31+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
32+
a("", "", "", ""); // Error
33+
~~~~~~~~~~~~~~~~~
34+
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [functionCallOnConstrainedTypeVariable.ts]
2+
// Repro from #20196
3+
4+
type A = {
5+
a: (x: number) => string
6+
};
7+
type B = {
8+
a: (x: boolean) => string
9+
};
10+
11+
function call0(p: A | B) {
12+
p.a("s"); // Error
13+
}
14+
15+
function callN<T extends A | B>(p: T) {
16+
p.a("s"); // Error
17+
18+
var a: T["a"] = p.a;
19+
a(""); // Error
20+
a("", "", "", ""); // Error
21+
}
22+
23+
//// [functionCallOnConstrainedTypeVariable.js]
24+
"use strict";
25+
// Repro from #20196
26+
function call0(p) {
27+
p.a("s"); // Error
28+
}
29+
function callN(p) {
30+
p.a("s"); // Error
31+
var a = p.a;
32+
a(""); // Error
33+
a("", "", "", ""); // Error
34+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=== tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts ===
2+
// Repro from #20196
3+
4+
type A = {
5+
>A : Symbol(A, Decl(functionCallOnConstrainedTypeVariable.ts, 0, 0))
6+
7+
a: (x: number) => string
8+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10))
9+
>x : Symbol(x, Decl(functionCallOnConstrainedTypeVariable.ts, 3, 6))
10+
11+
};
12+
type B = {
13+
>B : Symbol(B, Decl(functionCallOnConstrainedTypeVariable.ts, 4, 2))
14+
15+
a: (x: boolean) => string
16+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
17+
>x : Symbol(x, Decl(functionCallOnConstrainedTypeVariable.ts, 6, 6))
18+
19+
};
20+
21+
function call0(p: A | B) {
22+
>call0 : Symbol(call0, Decl(functionCallOnConstrainedTypeVariable.ts, 7, 2))
23+
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 9, 15))
24+
>A : Symbol(A, Decl(functionCallOnConstrainedTypeVariable.ts, 0, 0))
25+
>B : Symbol(B, Decl(functionCallOnConstrainedTypeVariable.ts, 4, 2))
26+
27+
p.a("s"); // Error
28+
>p.a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
29+
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 9, 15))
30+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
31+
}
32+
33+
function callN<T extends A | B>(p: T) {
34+
>callN : Symbol(callN, Decl(functionCallOnConstrainedTypeVariable.ts, 11, 1))
35+
>T : Symbol(T, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 15))
36+
>A : Symbol(A, Decl(functionCallOnConstrainedTypeVariable.ts, 0, 0))
37+
>B : Symbol(B, Decl(functionCallOnConstrainedTypeVariable.ts, 4, 2))
38+
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 32))
39+
>T : Symbol(T, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 15))
40+
41+
p.a("s"); // Error
42+
>p.a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
43+
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 32))
44+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
45+
46+
var a: T["a"] = p.a;
47+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 16, 5))
48+
>T : Symbol(T, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 15))
49+
>p.a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
50+
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 32))
51+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
52+
53+
a(""); // Error
54+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 16, 5))
55+
56+
a("", "", "", ""); // Error
57+
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 16, 5))
58+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
=== tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts ===
2+
// Repro from #20196
3+
4+
type A = {
5+
>A : A
6+
7+
a: (x: number) => string
8+
>a : (x: number) => string
9+
>x : number
10+
11+
};
12+
type B = {
13+
>B : B
14+
15+
a: (x: boolean) => string
16+
>a : (x: boolean) => string
17+
>x : boolean
18+
19+
};
20+
21+
function call0(p: A | B) {
22+
>call0 : (p: A | B) => void
23+
>p : A | B
24+
>A : A
25+
>B : B
26+
27+
p.a("s"); // Error
28+
>p.a("s") : any
29+
>p.a : ((x: number) => string) | ((x: boolean) => string)
30+
>p : A | B
31+
>a : ((x: number) => string) | ((x: boolean) => string)
32+
>"s" : "s"
33+
}
34+
35+
function callN<T extends A | B>(p: T) {
36+
>callN : <T extends A | B>(p: T) => void
37+
>T : T
38+
>A : A
39+
>B : B
40+
>p : T
41+
>T : T
42+
43+
p.a("s"); // Error
44+
>p.a("s") : any
45+
>p.a : ((x: number) => string) | ((x: boolean) => string)
46+
>p : T
47+
>a : ((x: number) => string) | ((x: boolean) => string)
48+
>"s" : "s"
49+
50+
var a: T["a"] = p.a;
51+
>a : T["a"]
52+
>T : T
53+
>p.a : ((x: number) => string) | ((x: boolean) => string)
54+
>p : T
55+
>a : ((x: number) => string) | ((x: boolean) => string)
56+
57+
a(""); // Error
58+
>a("") : any
59+
>a : T["a"]
60+
>"" : ""
61+
62+
a("", "", "", ""); // Error
63+
>a("", "", "", "") : any
64+
>a : T["a"]
65+
>"" : ""
66+
>"" : ""
67+
>"" : ""
68+
>"" : ""
69+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @strict: true
2+
3+
// Repro from #20196
4+
5+
type A = {
6+
a: (x: number) => string
7+
};
8+
type B = {
9+
a: (x: boolean) => string
10+
};
11+
12+
function call0(p: A | B) {
13+
p.a("s"); // Error
14+
}
15+
16+
function callN<T extends A | B>(p: T) {
17+
p.a("s"); // Error
18+
19+
var a: T["a"] = p.a;
20+
a(""); // Error
21+
a("", "", "", ""); // Error
22+
}

0 commit comments

Comments
 (0)