Skip to content

Prevent substitution types from leaking out of non-deferred conditionals #54894

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17951,6 +17951,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
result.aliasTypeArguments = aliasSymbol ? aliasTypeArguments : instantiateTypes(root.aliasTypeArguments, mapper!); // TODO: GH#18217
break;
}
if (result.flags & TypeFlags.Substitution) {
const substitution = result as SubstitutionType;
result = getIntersectionType([substitution.baseType, substitution.constraint]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use getSubstitutionIntersection, if this sticks around.

}
return extraTypes ? getUnionType(append(extraTypes, result)) : result;
// We tail-recurse for generic conditional types that (a) have not already been evaluated and cached, and
// (b) are non distributive, have a check type that is unaffected by instantiation, or have a non-union check
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/substitutionTypeUsedAsIndex.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/substitutionTypeUsedAsIndex.ts] ////

=== substitutionTypeUsedAsIndex.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54886

interface Dict_54886 {
>Dict_54886 : Symbol(Dict_54886, Decl(substitutionTypeUsedAsIndex.ts, 0, 0))

foo: 1;
>foo : Symbol(Dict_54886.foo, Decl(substitutionTypeUsedAsIndex.ts, 2, 22))

bar: 1;
>bar : Symbol(Dict_54886.bar, Decl(substitutionTypeUsedAsIndex.ts, 3, 9))
}

type FF_54886 = "foo" extends "foo" | "bar" ? "foo" : never;
>FF_54886 : Symbol(FF_54886, Decl(substitutionTypeUsedAsIndex.ts, 5, 1))

type C_54886 = Dict_54886[FF_54886]; // ok
>C_54886 : Symbol(C_54886, Decl(substitutionTypeUsedAsIndex.ts, 7, 60))
>Dict_54886 : Symbol(Dict_54886, Decl(substitutionTypeUsedAsIndex.ts, 0, 0))
>FF_54886 : Symbol(FF_54886, Decl(substitutionTypeUsedAsIndex.ts, 5, 1))

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

=== substitutionTypeUsedAsIndex.ts ===
// repro from https://github.com/microsoft/TypeScript/issues/54886

interface Dict_54886 {
foo: 1;
>foo : 1

bar: 1;
>bar : 1
}

type FF_54886 = "foo" extends "foo" | "bar" ? "foo" : never;
>FF_54886 : "foo"

type C_54886 = Dict_54886[FF_54886]; // ok
>C_54886 : 1

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

// repro from https://github.com/microsoft/TypeScript/issues/54886

interface Dict_54886 {
foo: 1;
bar: 1;
}

type FF_54886 = "foo" extends "foo" | "bar" ? "foo" : never;
type C_54886 = Dict_54886[FF_54886]; // ok