Skip to content

Don't defer index types for non-generic substitution types #61999

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 1 commit 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
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18903,6 +18903,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function getIndexType(type: Type, indexFlags = IndexFlags.None): Type {
type = getReducedType(type);
return isNoInferType(type) ? getNoInferType(getIndexType((type as SubstitutionType).baseType, indexFlags)) :
type.flags & TypeFlags.Substitution && !isGenericType((type as SubstitutionType).baseType) && !isGenericType((type as SubstitutionType).constraint) ? getUnionType([getIndexType((type as SubstitutionType).baseType, indexFlags), getIndexType((type as SubstitutionType).constraint, indexFlags)]) :
shouldDeferIndexType(type, indexFlags) ? getIndexTypeForGenericType(type as InstantiableType | UnionOrIntersectionType, indexFlags) :
type.flags & TypeFlags.Union ? getIntersectionType(map((type as UnionType).types, t => getIndexType(t, indexFlags))) :
type.flags & TypeFlags.Intersection ? getUnionType(map((type as IntersectionType).types, t => getIndexType(t, indexFlags))) :
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/compiler/substitutionTypeNonGenericIndexType1.ts] ////

=== substitutionTypeNonGenericIndexType1.ts ===
// https://github.com/microsoft/TypeScript/issues/61728

type BasicConditional<T> = keyof T extends any
>BasicConditional : Symbol(BasicConditional, Decl(substitutionTypeNonGenericIndexType1.ts, 0, 0))
>T : Symbol(T, Decl(substitutionTypeNonGenericIndexType1.ts, 2, 22))
>T : Symbol(T, Decl(substitutionTypeNonGenericIndexType1.ts, 2, 22))

? true
: false;

type Config = { rejectClose: true };
>Config : Symbol(Config, Decl(substitutionTypeNonGenericIndexType1.ts, 4, 10))
>rejectClose : Symbol(rejectClose, Decl(substitutionTypeNonGenericIndexType1.ts, 6, 15))

type Test =
>Test : Symbol(Test, Decl(substitutionTypeNonGenericIndexType1.ts, 6, 36))

Config extends {}
>Config : Symbol(Config, Decl(substitutionTypeNonGenericIndexType1.ts, 4, 10))

? {
rejectClose: BasicConditional<Config>;
>rejectClose : Symbol(rejectClose, Decl(substitutionTypeNonGenericIndexType1.ts, 9, 7))
>BasicConditional : Symbol(BasicConditional, Decl(substitutionTypeNonGenericIndexType1.ts, 0, 0))
>Config : Symbol(Config, Decl(substitutionTypeNonGenericIndexType1.ts, 4, 10))
}
: never;

type RejectClose = Test["rejectClose"];
>RejectClose : Symbol(RejectClose, Decl(substitutionTypeNonGenericIndexType1.ts, 12, 12))
>Test : Symbol(Test, Decl(substitutionTypeNonGenericIndexType1.ts, 6, 36))

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

=== substitutionTypeNonGenericIndexType1.ts ===
// https://github.com/microsoft/TypeScript/issues/61728

type BasicConditional<T> = keyof T extends any
>BasicConditional : BasicConditional<T>
> : ^^^^^^^^^^^^^^^^^^^

? true
>true : true
> : ^^^^

: false;
>false : false
> : ^^^^^

type Config = { rejectClose: true };
>Config : Config
> : ^^^^^^
>rejectClose : true
> : ^^^^
>true : true
> : ^^^^

type Test =
>Test : { rejectClose: BasicConditional<Config>; }
> : ^^^^^^^^^^^^^^^ ^^^

Config extends {}
? {
rejectClose: BasicConditional<Config>;
>rejectClose : true
> : ^^^^
}
: never;

type RejectClose = Test["rejectClose"];
>RejectClose : true
> : ^^^^

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

=== substitutionTypeNonGenericIndexType2.ts ===
type BasicConditional<T> = keyof T extends infer R ? R : never;
>BasicConditional : Symbol(BasicConditional, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 0))
>T : Symbol(T, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 22))
>T : Symbol(T, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 22))
>R : Symbol(R, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 48))
>R : Symbol(R, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 48))

type Config = { rejectClose: true };
>Config : Symbol(Config, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 63))
>rejectClose : Symbol(rejectClose, Decl(substitutionTypeNonGenericIndexType2.ts, 2, 15))

type Test = Config extends {}
>Test : Symbol(Test, Decl(substitutionTypeNonGenericIndexType2.ts, 2, 36))
>Config : Symbol(Config, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 63))

? {
rejectClose: BasicConditional<Config>;
>rejectClose : Symbol(rejectClose, Decl(substitutionTypeNonGenericIndexType2.ts, 5, 5))
>BasicConditional : Symbol(BasicConditional, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 0))
>Config : Symbol(Config, Decl(substitutionTypeNonGenericIndexType2.ts, 0, 63))
}
: never;

const test: Test["rejectClose"] = "rejectClose";
>test : Symbol(test, Decl(substitutionTypeNonGenericIndexType2.ts, 10, 5))
>Test : Symbol(Test, Decl(substitutionTypeNonGenericIndexType2.ts, 2, 36))

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

=== substitutionTypeNonGenericIndexType2.ts ===
type BasicConditional<T> = keyof T extends infer R ? R : never;
>BasicConditional : BasicConditional<T>
> : ^^^^^^^^^^^^^^^^^^^

type Config = { rejectClose: true };
>Config : Config
> : ^^^^^^
>rejectClose : true
> : ^^^^
>true : true
> : ^^^^

type Test = Config extends {}
>Test : { rejectClose: BasicConditional<Config>; }
> : ^^^^^^^^^^^^^^^ ^^^

? {
rejectClose: BasicConditional<Config>;
>rejectClose : "rejectClose"
> : ^^^^^^^^^^^^^
}
: never;

const test: Test["rejectClose"] = "rejectClose";
>test : "rejectClose"
> : ^^^^^^^^^^^^^
>"rejectClose" : "rejectClose"
> : ^^^^^^^^^^^^^

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

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

type BasicConditional<T> = keyof T extends any
? true
: false;

type Config = { rejectClose: true };
type Test =
Config extends {}
? {
rejectClose: BasicConditional<Config>;
}
: never;

type RejectClose = Test["rejectClose"];
14 changes: 14 additions & 0 deletions tests/cases/compiler/substitutionTypeNonGenericIndexType2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true
// @noEmit: true

type BasicConditional<T> = keyof T extends infer R ? R : never;

type Config = { rejectClose: true };

type Test = Config extends {}
? {
rejectClose: BasicConditional<Config>;
}
: never;

const test: Test["rejectClose"] = "rejectClose";