diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 86702764c9585..3387f187620c6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -37142,8 +37142,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function getInstantiatedSignatures(signatures: readonly Signature[]) { - const applicableSignatures = filter(signatures, sig => !!sig.typeParameters && hasCorrectTypeArgumentArity(sig, typeArguments)); - return sameMap(applicableSignatures, sig => { + const sameTypeAritySignatures = filter(signatures, sig => !!sig.typeParameters && hasCorrectTypeArgumentArity(sig, typeArguments)); + if (!sameTypeAritySignatures.length) { + return sameTypeAritySignatures; + } + const applicableSignatures = mapDefined(sameTypeAritySignatures, sig => { + const typeArgumentTypes = checkTypeArguments(sig, typeArguments!, /*reportErrors*/ false); + return typeArgumentTypes && getSignatureInstantiation(sig, typeArgumentTypes, isInJSFile(sig.declaration)); + }); + if (applicableSignatures.length) { + return applicableSignatures; + } + return sameMap(sameTypeAritySignatures, sig => { const typeArgumentTypes = checkTypeArguments(sig, typeArguments!, /*reportErrors*/ true); return typeArgumentTypes ? getSignatureInstantiation(sig, typeArgumentTypes, isInJSFile(sig.declaration)) : sig; }); diff --git a/tests/baselines/reference/instantiationExpressions.errors.txt b/tests/baselines/reference/instantiationExpressions.errors.txt index 3412fd4cbc4e4..bed3295439456 100644 --- a/tests/baselines/reference/instantiationExpressions.errors.txt +++ b/tests/baselines/reference/instantiationExpressions.errors.txt @@ -225,4 +225,14 @@ instantiationExpressions.ts(164,40): error TS2344: Type 'U' does not satisfy the type T50 = typeof g3; // (a: U) => U type T51 = typeof g3; // (b: U) => U + + // repro #47607#issuecomment-1331744280 + + type DivElement = { type: 'div' } + interface ElementMap { div: DivElement } + + declare function foo(arg: T): ElementMap[T]; + declare function foo(arg: T): T; + + type DivFromMap = typeof foo<"div">; \ No newline at end of file diff --git a/tests/baselines/reference/instantiationExpressions.js b/tests/baselines/reference/instantiationExpressions.js index d44183036cf55..e2a49096ed031 100644 --- a/tests/baselines/reference/instantiationExpressions.js +++ b/tests/baselines/reference/instantiationExpressions.js @@ -173,6 +173,16 @@ declare const g3: { type T50 = typeof g3; // (a: U) => U type T51 = typeof g3; // (b: U) => U + +// repro #47607#issuecomment-1331744280 + +type DivElement = { type: 'div' } +interface ElementMap { div: DivElement } + +declare function foo(arg: T): ElementMap[T]; +declare function foo(arg: T): T; + +type DivFromMap = typeof foo<"div">; //// [instantiationExpressions.js] @@ -382,3 +392,12 @@ declare const g3: { }; type T50 = typeof g3; type T51 = typeof g3; +type DivElement = { + type: 'div'; +}; +interface ElementMap { + div: DivElement; +} +declare function foo(arg: T): ElementMap[T]; +declare function foo(arg: T): T; +type DivFromMap = typeof foo<"div">; diff --git a/tests/baselines/reference/instantiationExpressions.symbols b/tests/baselines/reference/instantiationExpressions.symbols index 81cd051674299..ecd3aaa79c6e8 100644 --- a/tests/baselines/reference/instantiationExpressions.symbols +++ b/tests/baselines/reference/instantiationExpressions.symbols @@ -650,3 +650,35 @@ type T51 = typeof g3; // (b: U) => U >g3 : Symbol(g3, Decl(instantiationExpressions.ts, 165, 13)) >U : Symbol(U, Decl(instantiationExpressions.ts, 171, 9)) +// repro #47607#issuecomment-1331744280 + +type DivElement = { type: 'div' } +>DivElement : Symbol(DivElement, Decl(instantiationExpressions.ts, 171, 47)) +>type : Symbol(type, Decl(instantiationExpressions.ts, 175, 19)) + +interface ElementMap { div: DivElement } +>ElementMap : Symbol(ElementMap, Decl(instantiationExpressions.ts, 175, 33)) +>div : Symbol(ElementMap.div, Decl(instantiationExpressions.ts, 176, 22)) +>DivElement : Symbol(DivElement, Decl(instantiationExpressions.ts, 171, 47)) + +declare function foo(arg: T): ElementMap[T]; +>foo : Symbol(foo, Decl(instantiationExpressions.ts, 176, 40), Decl(instantiationExpressions.ts, 178, 72)) +>T : Symbol(T, Decl(instantiationExpressions.ts, 178, 21)) +>ElementMap : Symbol(ElementMap, Decl(instantiationExpressions.ts, 175, 33)) +>arg : Symbol(arg, Decl(instantiationExpressions.ts, 178, 49)) +>T : Symbol(T, Decl(instantiationExpressions.ts, 178, 21)) +>ElementMap : Symbol(ElementMap, Decl(instantiationExpressions.ts, 175, 33)) +>T : Symbol(T, Decl(instantiationExpressions.ts, 178, 21)) + +declare function foo(arg: T): T; +>foo : Symbol(foo, Decl(instantiationExpressions.ts, 176, 40), Decl(instantiationExpressions.ts, 178, 72)) +>T : Symbol(T, Decl(instantiationExpressions.ts, 179, 21)) +>DivElement : Symbol(DivElement, Decl(instantiationExpressions.ts, 171, 47)) +>arg : Symbol(arg, Decl(instantiationExpressions.ts, 179, 43)) +>T : Symbol(T, Decl(instantiationExpressions.ts, 179, 21)) +>T : Symbol(T, Decl(instantiationExpressions.ts, 179, 21)) + +type DivFromMap = typeof foo<"div">; +>DivFromMap : Symbol(DivFromMap, Decl(instantiationExpressions.ts, 179, 54)) +>foo : Symbol(foo, Decl(instantiationExpressions.ts, 176, 40), Decl(instantiationExpressions.ts, 178, 72)) + diff --git a/tests/baselines/reference/instantiationExpressions.types b/tests/baselines/reference/instantiationExpressions.types index 4b3156a650df7..e02b84e943bd3 100644 --- a/tests/baselines/reference/instantiationExpressions.types +++ b/tests/baselines/reference/instantiationExpressions.types @@ -802,3 +802,33 @@ type T51 = typeof g3; // (b: U) => U >g3 : { (a: T): T; new (b: T): T; } > : ^^^ ^^^^^^^^^ ^^ ^^ ^^^ ^^^^^^^ ^^^^^^^^^ ^^ ^^ ^^ ^^^ ^^^ +// repro #47607#issuecomment-1331744280 + +type DivElement = { type: 'div' } +>DivElement : DivElement +> : ^^^^^^^^^^ +>type : "div" +> : ^^^^^ + +interface ElementMap { div: DivElement } +>div : DivElement +> : ^^^^^^^^^^ + +declare function foo(arg: T): ElementMap[T]; +>foo : { (arg: T): ElementMap[T]; (arg: T_1): T_1; } +> : ^^^ ^^^^^^^^^ ^^ ^^ ^^^ ^^^^^^^^^^^^^^^ ^^ ^^ ^^^ ^^^ +>arg : T +> : ^ + +declare function foo(arg: T): T; +>foo : { (arg: T_1): ElementMap[T_1]; (arg: T): T; } +> : ^^^^^^^^^^^^^^^ ^^ ^^ ^^^ ^^^ ^^^^^^^^^ ^^ ^^ ^^^ ^^^ +>arg : T +> : ^ + +type DivFromMap = typeof foo<"div">; +>DivFromMap : typeof foo<"div"> +> : +>foo : { (arg: T): ElementMap[T]; (arg: T): T; } +> : ^^^ ^^^^^^^^^ ^^ ^^ ^^^ ^^^ ^^^^^^^^^ ^^ ^^ ^^^ ^^^ + diff --git a/tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressions.ts b/tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressions.ts index ce9d23aacc96d..a053e20009a03 100644 --- a/tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressions.ts +++ b/tests/cases/conformance/types/typeParameters/typeArgumentLists/instantiationExpressions.ts @@ -173,3 +173,13 @@ declare const g3: { type T50 = typeof g3; // (a: U) => U type T51 = typeof g3; // (b: U) => U + +// repro #47607#issuecomment-1331744280 + +type DivElement = { type: 'div' } +interface ElementMap { div: DivElement } + +declare function foo(arg: T): ElementMap[T]; +declare function foo(arg: T): T; + +type DivFromMap = typeof foo<"div">;