Skip to content

Don't erase signature type parameters in signaturesRelatedTo #48092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15276,7 +15276,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return undefined;
}

function getSignatureInstantiation(signature: Signature, typeArguments: Type[] | undefined, isJavascript: boolean, inferredTypeParameters?: readonly TypeParameter[]): Signature {
function getSignatureInstantiation(signature: Signature, typeArguments: readonly Type[] | undefined, isJavascript: boolean, inferredTypeParameters?: readonly TypeParameter[]): Signature {
const instantiatedSignature = getSignatureInstantiationWithoutFillingInTypeArguments(signature, fillMissingTypeArguments(typeArguments, signature.typeParameters, getMinTypeArgumentCount(signature.typeParameters), isJavascript));
if (inferredTypeParameters) {
const returnSignature = getSingleCallOrConstructSignature(getReturnTypeOfSignature(instantiatedSignature));
Expand Down Expand Up @@ -22963,10 +22963,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
) {
// We have instantiations of the same anonymous type (which typically will be the type of a
// method). Simply do a pairwise comparison of the signatures in the two signature lists instead
// of the much more expensive N * M comparison matrix we explore below. We erase type parameters
// as they are known to always be the same.
// of the much more expensive N * M comparison matrix we explore below. We instantiate the source
// signature with the type parameters of the target signature to unify the two.
for (let i = 0; i < targetSignatures.length; i++) {
const related = signatureRelatedTo(sourceSignatures[i], targetSignatures[i], /*erase*/ true, reportErrors, intersectionState, incompatibleReporter(sourceSignatures[i], targetSignatures[i]));
const sourceSignature = sourceSignatures[i];
const targetSignature = targetSignatures[i];
const instantiatedSourceSignature = targetSignature.typeParameters ?
getSignatureInstantiation(sourceSignature, targetSignature.typeParameters, isInJSFile(sourceSignature.declaration)) :
sourceSignature;
const related = signatureRelatedTo(instantiatedSourceSignature, targetSignature, /*erase*/ false, reportErrors, intersectionState, incompatibleReporter(instantiatedSourceSignature, targetSignature));
if (!related) {
return Ternary.False;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ declare module Immutable {
>Seq : typeof Seq

function isSeq(maybeSeq: any): maybeSeq is Seq.Indexed<any> | Seq.Keyed<any, any>;
>isSeq : (maybeSeq: any) => maybeSeq is Indexed<any> | Keyed<any, any>
>isSeq : (maybeSeq: any) => maybeSeq is Keyed<any, any> | Indexed<any>
>maybeSeq : any
>Seq : any
>Seq : any
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/genericSignatureRelations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/genericSignatureRelations.ts] ////

//// [genericSignatureRelations.ts]
// Repro from #48070

type S<X> = <T>() => T extends X ? 1 : '2';

type Foo1 = S<'s1'>;
type Foo2 = S<'s2'>;

type Result1 = Foo1 extends Foo2 ? true : false;
type Result2 = S<'s1'> extends S<'s2'> ? true : false;


//// [genericSignatureRelations.js]
"use strict";
// Repro from #48070


//// [genericSignatureRelations.d.ts]
type S<X> = <T>() => T extends X ? 1 : '2';
type Foo1 = S<'s1'>;
type Foo2 = S<'s2'>;
type Result1 = Foo1 extends Foo2 ? true : false;
type Result2 = S<'s1'> extends S<'s2'> ? true : false;
30 changes: 30 additions & 0 deletions tests/baselines/reference/genericSignatureRelations.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/genericSignatureRelations.ts] ////

=== genericSignatureRelations.ts ===
// Repro from #48070

type S<X> = <T>() => T extends X ? 1 : '2';
>S : Symbol(S, Decl(genericSignatureRelations.ts, 0, 0))
>X : Symbol(X, Decl(genericSignatureRelations.ts, 2, 7))
>T : Symbol(T, Decl(genericSignatureRelations.ts, 2, 13))
>T : Symbol(T, Decl(genericSignatureRelations.ts, 2, 13))
>X : Symbol(X, Decl(genericSignatureRelations.ts, 2, 7))

type Foo1 = S<'s1'>;
>Foo1 : Symbol(Foo1, Decl(genericSignatureRelations.ts, 2, 43))
>S : Symbol(S, Decl(genericSignatureRelations.ts, 0, 0))

type Foo2 = S<'s2'>;
>Foo2 : Symbol(Foo2, Decl(genericSignatureRelations.ts, 4, 20))
>S : Symbol(S, Decl(genericSignatureRelations.ts, 0, 0))

type Result1 = Foo1 extends Foo2 ? true : false;
>Result1 : Symbol(Result1, Decl(genericSignatureRelations.ts, 5, 20))
>Foo1 : Symbol(Foo1, Decl(genericSignatureRelations.ts, 2, 43))
>Foo2 : Symbol(Foo2, Decl(genericSignatureRelations.ts, 4, 20))

type Result2 = S<'s1'> extends S<'s2'> ? true : false;
>Result2 : Symbol(Result2, Decl(genericSignatureRelations.ts, 7, 48))
>S : Symbol(S, Decl(genericSignatureRelations.ts, 0, 0))
>S : Symbol(S, Decl(genericSignatureRelations.ts, 0, 0))

24 changes: 24 additions & 0 deletions tests/baselines/reference/genericSignatureRelations.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/compiler/genericSignatureRelations.ts] ////

=== genericSignatureRelations.ts ===
// Repro from #48070

type S<X> = <T>() => T extends X ? 1 : '2';
>S : S<X>

type Foo1 = S<'s1'>;
>Foo1 : <T>() => T extends "s1" ? 1 : "2"

type Foo2 = S<'s2'>;
>Foo2 : <T>() => T extends "s2" ? 1 : "2"

type Result1 = Foo1 extends Foo2 ? true : false;
>Result1 : false
>true : true
>false : false

type Result2 = S<'s1'> extends S<'s2'> ? true : false;
>Result2 : false
>true : true
>false : false

12 changes: 12 additions & 0 deletions tests/cases/compiler/genericSignatureRelations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @strict: true
// @declaration: true

// Repro from #48070

type S<X> = <T>() => T extends X ? 1 : '2';

type Foo1 = S<'s1'>;
type Foo2 = S<'s2'>;

type Result1 = Foo1 extends Foo2 ? true : false;
type Result2 = S<'s1'> extends S<'s2'> ? true : false;