Skip to content
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
15 changes: 12 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16579,10 +16579,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
type.flags & TypeFlags.Intersection && maybeTypeOfKind(type, TypeFlags.Instantiable) && some((type as IntersectionType).types, isEmptyAnonymousObjectType));
}

function getIndexType(type: Type, stringsOnly = keyofStringsOnly, noIndexSignatures?: boolean): Type {
function getImmediateIndexType(type: Type, stringsOnly = keyofStringsOnly, noIndexSignatures?: boolean): Type {
type = getReducedType(type);
return shouldDeferIndexType(type) ? getIndexTypeForGenericType(type as InstantiableType | UnionOrIntersectionType, stringsOnly) :
type.flags & TypeFlags.Union ? getIntersectionType(map((type as UnionType).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) :
return type.flags & TypeFlags.Union ? getIntersectionType(map((type as UnionType).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) :
type.flags & TypeFlags.Intersection ? getUnionType(map((type as IntersectionType).types, t => getIndexType(t, stringsOnly, noIndexSignatures))) :
getObjectFlags(type) & ObjectFlags.Mapped ? getIndexTypeForMappedType(type as MappedType, stringsOnly, noIndexSignatures) :
type === wildcardType ? wildcardType :
Expand All @@ -16592,6 +16591,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
stringsOnly === keyofStringsOnly && !noIndexSignatures);
}

function getIndexType(type: Type, stringsOnly = keyofStringsOnly, noIndexSignatures?: boolean): Type {
type = getReducedType(type);
return shouldDeferIndexType(type)
? getIndexTypeForGenericType(type as InstantiableType | UnionOrIntersectionType, stringsOnly)
: getImmediateIndexType(type, stringsOnly, noIndexSignatures);
}

function getExtractStringType(type: Type) {
if (keyofStringsOnly) {
return type;
Expand Down Expand Up @@ -37473,6 +37479,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return type;
}
if (isGenericObjectType(objectType)) {
if (isTypeAssignableTo(indexType, getImmediateIndexType(apparentObjectType, /*stringsOnly*/ false))) {
return type;
}
const propertyName = getPropertyNameFromIndex(indexType, accessNode);
if (propertyName) {
const propertySymbol = forEachType(apparentObjectType, t => getPropertyOfType(t, propertyName));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/indexedAccessWithKnownKeyOnDeferrable.ts ===
// repro #51161

export type AnyOneof = { oneofKind: string; [k: string]: unknown } | { oneofKind: undefined };
>AnyOneof : Symbol(AnyOneof, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 0, 0))
>oneofKind : Symbol(oneofKind, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 2, 24))
>k : Symbol(k, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 2, 45))
>oneofKind : Symbol(oneofKind, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 2, 70))

export type AnyOneofKind<T extends AnyOneof> = T extends { oneofKind: keyof T }
>AnyOneofKind : Symbol(AnyOneofKind, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 2, 94))
>T : Symbol(T, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 3, 25))
>AnyOneof : Symbol(AnyOneof, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 0, 0))
>T : Symbol(T, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 3, 25))
>oneofKind : Symbol(oneofKind, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 3, 58))
>T : Symbol(T, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 3, 25))

? T['oneofKind']
>T : Symbol(T, Decl(indexedAccessWithKnownKeyOnDeferrable.ts, 3, 25))

: never;

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/indexedAccessWithKnownKeyOnDeferrable.ts ===
// repro #51161

export type AnyOneof = { oneofKind: string; [k: string]: unknown } | { oneofKind: undefined };
>AnyOneof : { [k: string]: unknown; oneofKind: string; } | { oneofKind: undefined; }
>oneofKind : string
>k : string
>oneofKind : undefined

export type AnyOneofKind<T extends AnyOneof> = T extends { oneofKind: keyof T }
>AnyOneofKind : AnyOneofKind<T>
>oneofKind : keyof T

? T['oneofKind']
: never;

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

// repro #51161

export type AnyOneof = { oneofKind: string; [k: string]: unknown } | { oneofKind: undefined };
export type AnyOneofKind<T extends AnyOneof> = T extends { oneofKind: keyof T }
? T['oneofKind']
: never;