Merge pull request #20404 from Microsoft/fixUntypedFunctionCall
Fix untyped function call on constrained type variable
This commit is contained in:
commit
f6b1a1de59
6 changed files with 219 additions and 1 deletions
|
@ -17115,7 +17115,7 @@ namespace ts {
|
|||
function isUntypedFunctionCall(funcType: Type, apparentFuncType: Type, numCallSignatures: number, numConstructSignatures: number) {
|
||||
// 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);
|
||||
!numCallSignatures && !numConstructSignatures && !(apparentFuncType.flags & (TypeFlags.Union | TypeFlags.Never)) && isTypeAssignableTo(funcType, globalFunctionType);
|
||||
}
|
||||
|
||||
function resolveNewExpression(node: NewExpression, candidatesOutArray: Signature[]): Signature {
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
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.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
|
||||
|
||||
==== tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts (4 errors) ====
|
||||
// Repro from #20196
|
||||
|
||||
type A = {
|
||||
a: (x: number) => string
|
||||
};
|
||||
type B = {
|
||||
a: (x: boolean) => string
|
||||
};
|
||||
|
||||
function call0(p: A | B) {
|
||||
p.a("s"); // Error
|
||||
~~~~~~~~
|
||||
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
|
||||
}
|
||||
|
||||
function callN<T extends A | B>(p: T) {
|
||||
p.a("s"); // Error
|
||||
~~~~~~~~
|
||||
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
|
||||
|
||||
var a: T["a"] = p.a;
|
||||
a(""); // Error
|
||||
~~~~~
|
||||
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
|
||||
a("", "", "", ""); // Error
|
||||
~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((x: number) => string) | ((x: boolean) => string)' has no compatible call signatures.
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
//// [functionCallOnConstrainedTypeVariable.ts]
|
||||
// Repro from #20196
|
||||
|
||||
type A = {
|
||||
a: (x: number) => string
|
||||
};
|
||||
type B = {
|
||||
a: (x: boolean) => string
|
||||
};
|
||||
|
||||
function call0(p: A | B) {
|
||||
p.a("s"); // Error
|
||||
}
|
||||
|
||||
function callN<T extends A | B>(p: T) {
|
||||
p.a("s"); // Error
|
||||
|
||||
var a: T["a"] = p.a;
|
||||
a(""); // Error
|
||||
a("", "", "", ""); // Error
|
||||
}
|
||||
|
||||
//// [functionCallOnConstrainedTypeVariable.js]
|
||||
"use strict";
|
||||
// Repro from #20196
|
||||
function call0(p) {
|
||||
p.a("s"); // Error
|
||||
}
|
||||
function callN(p) {
|
||||
p.a("s"); // Error
|
||||
var a = p.a;
|
||||
a(""); // Error
|
||||
a("", "", "", ""); // Error
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
=== tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts ===
|
||||
// Repro from #20196
|
||||
|
||||
type A = {
|
||||
>A : Symbol(A, Decl(functionCallOnConstrainedTypeVariable.ts, 0, 0))
|
||||
|
||||
a: (x: number) => string
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10))
|
||||
>x : Symbol(x, Decl(functionCallOnConstrainedTypeVariable.ts, 3, 6))
|
||||
|
||||
};
|
||||
type B = {
|
||||
>B : Symbol(B, Decl(functionCallOnConstrainedTypeVariable.ts, 4, 2))
|
||||
|
||||
a: (x: boolean) => string
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
|
||||
>x : Symbol(x, Decl(functionCallOnConstrainedTypeVariable.ts, 6, 6))
|
||||
|
||||
};
|
||||
|
||||
function call0(p: A | B) {
|
||||
>call0 : Symbol(call0, Decl(functionCallOnConstrainedTypeVariable.ts, 7, 2))
|
||||
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 9, 15))
|
||||
>A : Symbol(A, Decl(functionCallOnConstrainedTypeVariable.ts, 0, 0))
|
||||
>B : Symbol(B, Decl(functionCallOnConstrainedTypeVariable.ts, 4, 2))
|
||||
|
||||
p.a("s"); // Error
|
||||
>p.a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
|
||||
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 9, 15))
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
|
||||
}
|
||||
|
||||
function callN<T extends A | B>(p: T) {
|
||||
>callN : Symbol(callN, Decl(functionCallOnConstrainedTypeVariable.ts, 11, 1))
|
||||
>T : Symbol(T, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 15))
|
||||
>A : Symbol(A, Decl(functionCallOnConstrainedTypeVariable.ts, 0, 0))
|
||||
>B : Symbol(B, Decl(functionCallOnConstrainedTypeVariable.ts, 4, 2))
|
||||
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 32))
|
||||
>T : Symbol(T, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 15))
|
||||
|
||||
p.a("s"); // Error
|
||||
>p.a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
|
||||
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 32))
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
|
||||
|
||||
var a: T["a"] = p.a;
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 16, 5))
|
||||
>T : Symbol(T, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 15))
|
||||
>p.a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
|
||||
>p : Symbol(p, Decl(functionCallOnConstrainedTypeVariable.ts, 13, 32))
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 2, 10), Decl(functionCallOnConstrainedTypeVariable.ts, 5, 10))
|
||||
|
||||
a(""); // Error
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 16, 5))
|
||||
|
||||
a("", "", "", ""); // Error
|
||||
>a : Symbol(a, Decl(functionCallOnConstrainedTypeVariable.ts, 16, 5))
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
=== tests/cases/compiler/functionCallOnConstrainedTypeVariable.ts ===
|
||||
// Repro from #20196
|
||||
|
||||
type A = {
|
||||
>A : A
|
||||
|
||||
a: (x: number) => string
|
||||
>a : (x: number) => string
|
||||
>x : number
|
||||
|
||||
};
|
||||
type B = {
|
||||
>B : B
|
||||
|
||||
a: (x: boolean) => string
|
||||
>a : (x: boolean) => string
|
||||
>x : boolean
|
||||
|
||||
};
|
||||
|
||||
function call0(p: A | B) {
|
||||
>call0 : (p: A | B) => void
|
||||
>p : A | B
|
||||
>A : A
|
||||
>B : B
|
||||
|
||||
p.a("s"); // Error
|
||||
>p.a("s") : any
|
||||
>p.a : ((x: number) => string) | ((x: boolean) => string)
|
||||
>p : A | B
|
||||
>a : ((x: number) => string) | ((x: boolean) => string)
|
||||
>"s" : "s"
|
||||
}
|
||||
|
||||
function callN<T extends A | B>(p: T) {
|
||||
>callN : <T extends A | B>(p: T) => void
|
||||
>T : T
|
||||
>A : A
|
||||
>B : B
|
||||
>p : T
|
||||
>T : T
|
||||
|
||||
p.a("s"); // Error
|
||||
>p.a("s") : any
|
||||
>p.a : ((x: number) => string) | ((x: boolean) => string)
|
||||
>p : T
|
||||
>a : ((x: number) => string) | ((x: boolean) => string)
|
||||
>"s" : "s"
|
||||
|
||||
var a: T["a"] = p.a;
|
||||
>a : T["a"]
|
||||
>T : T
|
||||
>p.a : ((x: number) => string) | ((x: boolean) => string)
|
||||
>p : T
|
||||
>a : ((x: number) => string) | ((x: boolean) => string)
|
||||
|
||||
a(""); // Error
|
||||
>a("") : any
|
||||
>a : T["a"]
|
||||
>"" : ""
|
||||
|
||||
a("", "", "", ""); // Error
|
||||
>a("", "", "", "") : any
|
||||
>a : T["a"]
|
||||
>"" : ""
|
||||
>"" : ""
|
||||
>"" : ""
|
||||
>"" : ""
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
// @strict: true
|
||||
|
||||
// Repro from #20196
|
||||
|
||||
type A = {
|
||||
a: (x: number) => string
|
||||
};
|
||||
type B = {
|
||||
a: (x: boolean) => string
|
||||
};
|
||||
|
||||
function call0(p: A | B) {
|
||||
p.a("s"); // Error
|
||||
}
|
||||
|
||||
function callN<T extends A | B>(p: T) {
|
||||
p.a("s"); // Error
|
||||
|
||||
var a: T["a"] = p.a;
|
||||
a(""); // Error
|
||||
a("", "", "", ""); // Error
|
||||
}
|
Loading…
Reference in a new issue