Skip to content

Commit 40e0ab7

Browse files
authored
Merge pull request #23966 from Microsoft/fixIndexedAccessAnyConstraint
Fix indexed access with 'any' constraint
2 parents 27550d3 + 14e3e39 commit 40e0ab7

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10755,7 +10755,7 @@ namespace ts {
1075510755
}
1075610756
}
1075710757
const constraint = getConstraintForRelation(<TypeParameter>source);
10758-
if (!constraint || constraint.flags & TypeFlags.Any) {
10758+
if (!constraint || (source.flags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) {
1075910759
// A type variable with no constraint is not related to the non-primitive object type.
1076010760
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
1076110761
errorInfo = saveErrorInfo;

tests/baselines/reference/keyofAndIndexedAccess.js

+13
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,14 @@ class Unbounded<T> {
627627
let y: {} | undefined | null = x;
628628
}
629629
}
630+
631+
// Repro from #23940
632+
633+
interface I7 {
634+
x: any;
635+
}
636+
type Foo7<T extends number> = T;
637+
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;
630638

631639

632640
//// [keyofAndIndexedAccess.js]
@@ -1368,3 +1376,8 @@ declare function fn<T extends I, K extends keyof T>(o: T, k: K): void;
13681376
declare class Unbounded<T> {
13691377
foo(x: T[keyof T]): void;
13701378
}
1379+
interface I7 {
1380+
x: any;
1381+
}
1382+
declare type Foo7<T extends number> = T;
1383+
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;

tests/baselines/reference/keyofAndIndexedAccess.symbols

+23
Original file line numberDiff line numberDiff line change
@@ -2235,3 +2235,26 @@ class Unbounded<T> {
22352235
}
22362236
}
22372237

2238+
// Repro from #23940
2239+
2240+
interface I7 {
2241+
>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1))
2242+
2243+
x: any;
2244+
>x : Symbol(I7.x, Decl(keyofAndIndexedAccess.ts, 631, 14))
2245+
}
2246+
type Foo7<T extends number> = T;
2247+
>Foo7 : Symbol(Foo7, Decl(keyofAndIndexedAccess.ts, 633, 1))
2248+
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 634, 10))
2249+
>T : Symbol(T, Decl(keyofAndIndexedAccess.ts, 634, 10))
2250+
2251+
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;
2252+
>f7 : Symbol(f7, Decl(keyofAndIndexedAccess.ts, 634, 32))
2253+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20))
2254+
>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1))
2255+
>type : Symbol(type, Decl(keyofAndIndexedAccess.ts, 635, 40))
2256+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20))
2257+
>Foo7 : Symbol(Foo7, Decl(keyofAndIndexedAccess.ts, 633, 1))
2258+
>I7 : Symbol(I7, Decl(keyofAndIndexedAccess.ts, 627, 1))
2259+
>K : Symbol(K, Decl(keyofAndIndexedAccess.ts, 635, 20))
2260+

tests/baselines/reference/keyofAndIndexedAccess.types

+23
Original file line numberDiff line numberDiff line change
@@ -2591,3 +2591,26 @@ class Unbounded<T> {
25912591
}
25922592
}
25932593

2594+
// Repro from #23940
2595+
2596+
interface I7 {
2597+
>I7 : I7
2598+
2599+
x: any;
2600+
>x : any
2601+
}
2602+
type Foo7<T extends number> = T;
2603+
>Foo7 : T
2604+
>T : T
2605+
>T : T
2606+
2607+
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;
2608+
>f7 : <K extends "x">(type: K) => I7[K]
2609+
>K : K
2610+
>I7 : I7
2611+
>type : K
2612+
>K : K
2613+
>Foo7 : T
2614+
>I7 : I7
2615+
>K : K
2616+

tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts

+8
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,11 @@ class Unbounded<T> {
629629
let y: {} | undefined | null = x;
630630
}
631631
}
632+
633+
// Repro from #23940
634+
635+
interface I7 {
636+
x: any;
637+
}
638+
type Foo7<T extends number> = T;
639+
declare function f7<K extends keyof I7>(type: K): Foo7<I7[K]>;

0 commit comments

Comments
 (0)