Skip to content
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

Unwrap substitution types early in getPropertyTypeForIndexType #57113

Open
wants to merge 4 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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18538,6 +18538,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getPropertyTypeForIndexType(originalObjectType: Type, objectType: Type, indexType: Type, fullIndexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | PropertyName | BindingName | SyntheticExpression | undefined, accessFlags: AccessFlags) {
if (indexType.flags & TypeFlags.Substitution) {
indexType = (indexType as SubstitutionType).baseType;
}
const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined;
const propName = accessNode && isPrivateIdentifier(accessNode) ? undefined : getPropertyNameFromIndex(indexType, accessNode);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [tests/cases/compiler/nonGenericIndexedAccessInConditionalTrueBranch1.ts] ////

=== nonGenericIndexedAccessInConditionalTrueBranch1.ts ===
// https://github.com/microsoft/TypeScript/issues/57109

type A = {
>A : Symbol(A, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 0, 0))

k: symbol;
>k : Symbol(k, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 2, 10))

};

type B = "k" extends keyof A ? A["k"] : never;
>B : Symbol(B, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 4, 2))
>A : Symbol(A, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 0, 0))
>A : Symbol(A, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 0, 0))

type C = {
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46))

k: symbol;
>k : Symbol(k, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 8, 10))

other: boolean;
>other : Symbol(other, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 9, 12))

};

type D = "k" extends keyof C ? C["k"] : never;
>D : Symbol(D, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 11, 2))
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46))
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46))

type E = "k" extends keyof C ? C["k" | "other"] : never;
>E : Symbol(E, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 13, 46))
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46))
>C : Symbol(C, Decl(nonGenericIndexedAccessInConditionalTrueBranch1.ts, 6, 46))

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/compiler/nonGenericIndexedAccessInConditionalTrueBranch1.ts] ////

=== nonGenericIndexedAccessInConditionalTrueBranch1.ts ===
// https://github.com/microsoft/TypeScript/issues/57109

type A = {
>A : A
> : ^

k: symbol;
>k : symbol
> : ^^^^^^

};

type B = "k" extends keyof A ? A["k"] : never;
>B : symbol
> : ^^^^^^

type C = {
>C : C
> : ^

k: symbol;
>k : symbol
> : ^^^^^^

other: boolean;
>other : boolean
> : ^^^^^^^

};

type D = "k" extends keyof C ? C["k"] : never;
>D : symbol
> : ^^^^^^

type E = "k" extends keyof C ? C["k" | "other"] : never;
>E : boolean | symbol
> : ^^^^^^^^^^^^^^^^

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))

23 changes: 23 additions & 0 deletions tests/baselines/reference/substitutionTypeUsedAsIndex.types
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 {
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
> : ^

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @strict: true
// @noEmit: true

// https://github.com/microsoft/TypeScript/issues/57109

type A = {
k: symbol;
};

type B = "k" extends keyof A ? A["k"] : never;

type C = {
k: symbol;
other: boolean;
};

type D = "k" extends keyof C ? C["k"] : never;
type E = "k" extends keyof C ? C["k" | "other"] : never;
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