diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a34f073df3ec8..1c9de743e9bc9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10740,7 +10740,7 @@ namespace ts { } } const constraint = getConstraintForRelation(source); - if (!constraint || constraint.flags & TypeFlags.Any) { + if (!constraint || (source.flags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) { // A type variable with no constraint is not related to the non-primitive object type. if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) { errorInfo = saveErrorInfo; diff --git a/tests/baselines/reference/keyofAndIndexedAccess.js b/tests/baselines/reference/keyofAndIndexedAccess.js index 3d222583ffd4b..c23f10f3e68c6 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.js +++ b/tests/baselines/reference/keyofAndIndexedAccess.js @@ -627,6 +627,14 @@ class Unbounded { let y: {} | undefined | null = x; } } + +// Repro from #23940 + +interface I7 { + x: any; +} +type Foo7 = T; +declare function f7(type: K): Foo7; //// [keyofAndIndexedAccess.js] @@ -1368,3 +1376,8 @@ declare function fn(o: T, k: K): void; declare class Unbounded { foo(x: T[keyof T]): void; } +interface I7 { + x: any; +} +declare type Foo7 = T; +declare function f7(type: K): Foo7; diff --git a/tests/baselines/reference/keyofAndIndexedAccess.symbols b/tests/baselines/reference/keyofAndIndexedAccess.symbols index a40a2347d91a8..ef43a7112648b 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccess.symbols @@ -2235,3 +2235,26 @@ class Unbounded { } } +// Repro from #23940 + +interface I7 { +>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1)) + + x: any; +>x : Symbol(I7.x, Decl(keyofAndIndexedAccess.ts, 631, 14)) +} +type Foo7 = T; +>Foo7 : Symbol(Foo7, Decl(keyofAndIndexedAccess.ts, 633, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 634, 10)) +>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 634, 10)) + +declare function f7(type: K): Foo7; +>f7 : Symbol(f7, Decl(keyofAndIndexedAccess.ts, 634, 32)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20)) +>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1)) +>type : Symbol(type, Decl(keyofAndIndexedAccess.ts, 635, 40)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20)) +>Foo7 : Symbol(Foo7, Decl(keyofAndIndexedAccess.ts, 633, 1)) +>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1)) +>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20)) + diff --git a/tests/baselines/reference/keyofAndIndexedAccess.types b/tests/baselines/reference/keyofAndIndexedAccess.types index 0d6a350bd3ca2..a85b34d707b36 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.types +++ b/tests/baselines/reference/keyofAndIndexedAccess.types @@ -2591,3 +2591,26 @@ class Unbounded { } } +// Repro from #23940 + +interface I7 { +>I7 : I7 + + x: any; +>x : any +} +type Foo7 = T; +>Foo7 : T +>T : T +>T : T + +declare function f7(type: K): Foo7; +>f7 : (type: K) => I7[K] +>K : K +>I7 : I7 +>type : K +>K : K +>Foo7 : T +>I7 : I7 +>K : K + diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts index 83687e8c87251..07aa3c1994f0a 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts @@ -629,3 +629,11 @@ class Unbounded { let y: {} | undefined | null = x; } } + +// Repro from #23940 + +interface I7 { + x: any; +} +type Foo7 = T; +declare function f7(type: K): Foo7;