Skip to content

Commit cbf3c63

Browse files
authored
Revert overly permissive indexed access constraints (#54845)
1 parent 04bfd23 commit cbf3c63

6 files changed

+213
-12
lines changed

src/compiler/checker.ts

-11
Original file line numberDiff line numberDiff line change
@@ -21714,17 +21714,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2171421714
else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, RecursionFlags.Source, reportErrors && constraint !== unknownType && !(targetFlags & sourceFlags & TypeFlags.TypeParameter), /*headMessage*/ undefined, intersectionState)) {
2171521715
return result;
2171621716
}
21717-
if (sourceFlags & TypeFlags.IndexedAccess) {
21718-
const indexType = (source as IndexedAccessType).indexType;
21719-
if (indexType.flags & TypeFlags.Index) {
21720-
const unresolvedIndexConstraint = getBaseConstraintOfType((indexType as IndexType).type);
21721-
const indexConstraint = unresolvedIndexConstraint && unresolvedIndexConstraint !== noConstraintType ? getIndexType(unresolvedIndexConstraint) : keyofConstraintType;
21722-
const constraint = getIndexedAccessType((source as IndexedAccessType).objectType, indexConstraint);
21723-
if (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
21724-
return result;
21725-
}
21726-
}
21727-
}
2172821717
if (isMappedTypeGenericIndexedAccess(source)) {
2172921718
// For an indexed access type { [P in K]: E}[X], above we have already explored an instantiation of E with X
2173021719
// substituted for P. We also want to explore type { [P in K]: E }[C], where C is the constraint of X.

tests/baselines/reference/constraintWithIndexedAccess.errors.txt

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
constraintWithIndexedAccess.ts(23,90): error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: any) => any'.
2+
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
3+
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
4+
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
5+
constraintWithIndexedAccess.ts(24,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
6+
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
7+
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
8+
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
9+
constraintWithIndexedAccess.ts(26,103): error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: any) => any'.
10+
Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
11+
Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: any) => any'.
12+
Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: any) => any'.
13+
constraintWithIndexedAccess.ts(27,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
14+
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
15+
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
16+
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
117
constraintWithIndexedAccess.ts(28,102): error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: any) => any'.
218
Type 'DataFetchFns[T]["Boat"] | DataFetchFns[T]["Plane"]' is not assignable to type '(...args: any) => any'.
319
Type 'DataFetchFns[T]["Boat"]' is not assignable to type '(...args: any) => any'.
@@ -11,7 +27,7 @@ constraintWithIndexedAccess.ts(29,102): error TS2344: Type 'DataFetchFns[F][F]'
1127
constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to index type 'DataFetchFns[F]'.
1228

1329

14-
==== constraintWithIndexedAccess.ts (5 errors) ====
30+
==== constraintWithIndexedAccess.ts (9 errors) ====
1531
// #52399
1632
type DataFetchFns = {
1733
Boat: {
@@ -35,10 +51,30 @@ constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to
3551
export type returnTypeOfFunctions = ReturnType<allAreFunctionsAsExpected>; //string | number | boolean as expected
3652
export type SucceedingCombo = ReturnType<TypeHardcodedAsParameterWithoutReturnType<'Boat', keyof DataFetchFns['Boat']>>;
3753
export type FailingCombo<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<TypeHardcodedAsParameterWithoutReturnType<T,F>>;
54+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55+
!!! error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: any) => any'.
56+
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
57+
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
58+
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
3859
export type TypeHardcodedAsParameter<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;
60+
~~~~~~~~~~~~~~~~~~
61+
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
62+
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
63+
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
64+
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
3965
type VehicleSelector<T extends keyof DataFetchFns> = DataFetchFns[T];
4066
export type TypeHardcodedAsParameter2<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<VehicleSelector<T>[F]>;
67+
~~~~~~~~~~~~~~~~~~~~~
68+
!!! error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: any) => any'.
69+
!!! error TS2344: Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
70+
!!! error TS2344: Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: any) => any'.
71+
!!! error TS2344: Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: any) => any'.
4172
export type TypeGeneric1<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;
73+
~~~~~~~~~~~~~~~~~~
74+
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
75+
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
76+
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
77+
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
4278
export type TypeGeneric2<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][T]>; // error
4379
~~~~~~~~~~~~~~~~~~
4480
!!! error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: any) => any'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
indexedAccessConstraints.ts(2,9): error TS2322: Type 'T[keyof T]' is not assignable to type 'number'.
2+
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'number'.
3+
Type 'T[string]' is not assignable to type 'number'.
4+
5+
6+
==== indexedAccessConstraints.ts (1 errors) ====
7+
function foo<T extends object>(a: T[keyof T]) {
8+
let b: number = a; // Error
9+
~
10+
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'number'.
11+
!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'number'.
12+
!!! error TS2322: Type 'T[string]' is not assignable to type 'number'.
13+
}
14+
15+
// Repro from #54522
16+
17+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
18+
const fn = obj[methodKey];
19+
if (typeof fn !== 'function') {
20+
return 0;
21+
}
22+
return fn.length;
23+
}
24+
25+
// Repro from #54837
26+
27+
function getField<T extends object>(x: T | null, k: keyof T) {
28+
const result = x ? x[k] : null;
29+
return result; // T[keyof T] | null
30+
}
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//// [tests/cases/compiler/indexedAccessConstraints.ts] ////
2+
3+
=== indexedAccessConstraints.ts ===
4+
function foo<T extends object>(a: T[keyof T]) {
5+
>foo : Symbol(foo, Decl(indexedAccessConstraints.ts, 0, 0))
6+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13))
7+
>a : Symbol(a, Decl(indexedAccessConstraints.ts, 0, 31))
8+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13))
9+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 0, 13))
10+
11+
let b: number = a; // Error
12+
>b : Symbol(b, Decl(indexedAccessConstraints.ts, 1, 7))
13+
>a : Symbol(a, Decl(indexedAccessConstraints.ts, 0, 31))
14+
}
15+
16+
// Repro from #54522
17+
18+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
19+
>methodFnLength : Symbol(methodFnLength, Decl(indexedAccessConstraints.ts, 2, 1))
20+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31))
21+
>K : Symbol(K, Decl(indexedAccessConstraints.ts, 6, 44))
22+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31))
23+
>obj : Symbol(obj, Decl(indexedAccessConstraints.ts, 6, 64))
24+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 6, 31))
25+
>methodKey : Symbol(methodKey, Decl(indexedAccessConstraints.ts, 6, 71))
26+
>K : Symbol(K, Decl(indexedAccessConstraints.ts, 6, 44))
27+
28+
const fn = obj[methodKey];
29+
>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9))
30+
>obj : Symbol(obj, Decl(indexedAccessConstraints.ts, 6, 64))
31+
>methodKey : Symbol(methodKey, Decl(indexedAccessConstraints.ts, 6, 71))
32+
33+
if (typeof fn !== 'function') {
34+
>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9))
35+
36+
return 0;
37+
}
38+
return fn.length;
39+
>fn.length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --))
40+
>fn : Symbol(fn, Decl(indexedAccessConstraints.ts, 7, 9))
41+
>length : Symbol(Function.length, Decl(lib.es5.d.ts, --, --))
42+
}
43+
44+
// Repro from #54837
45+
46+
function getField<T extends object>(x: T | null, k: keyof T) {
47+
>getField : Symbol(getField, Decl(indexedAccessConstraints.ts, 12, 1))
48+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18))
49+
>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36))
50+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18))
51+
>k : Symbol(k, Decl(indexedAccessConstraints.ts, 16, 48))
52+
>T : Symbol(T, Decl(indexedAccessConstraints.ts, 16, 18))
53+
54+
const result = x ? x[k] : null;
55+
>result : Symbol(result, Decl(indexedAccessConstraints.ts, 17, 9))
56+
>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36))
57+
>x : Symbol(x, Decl(indexedAccessConstraints.ts, 16, 36))
58+
>k : Symbol(k, Decl(indexedAccessConstraints.ts, 16, 48))
59+
60+
return result; // T[keyof T] | null
61+
>result : Symbol(result, Decl(indexedAccessConstraints.ts, 17, 9))
62+
}
63+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//// [tests/cases/compiler/indexedAccessConstraints.ts] ////
2+
3+
=== indexedAccessConstraints.ts ===
4+
function foo<T extends object>(a: T[keyof T]) {
5+
>foo : <T extends object>(a: T[keyof T]) => void
6+
>a : T[keyof T]
7+
8+
let b: number = a; // Error
9+
>b : number
10+
>a : T[keyof T]
11+
}
12+
13+
// Repro from #54522
14+
15+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
16+
>methodFnLength : <T extends {}, K extends keyof T>(obj: T, methodKey: K) => number
17+
>obj : T
18+
>methodKey : K
19+
20+
const fn = obj[methodKey];
21+
>fn : T[K]
22+
>obj[methodKey] : T[K]
23+
>obj : T
24+
>methodKey : K
25+
26+
if (typeof fn !== 'function') {
27+
>typeof fn !== 'function' : boolean
28+
>typeof fn : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
29+
>fn : T[K]
30+
>'function' : "function"
31+
32+
return 0;
33+
>0 : 0
34+
}
35+
return fn.length;
36+
>fn.length : number
37+
>fn : T[K] & Function
38+
>length : number
39+
}
40+
41+
// Repro from #54837
42+
43+
function getField<T extends object>(x: T | null, k: keyof T) {
44+
>getField : <T extends object>(x: T | null, k: keyof T) => T[keyof T] | null
45+
>x : T | null
46+
>k : keyof T
47+
48+
const result = x ? x[k] : null;
49+
>result : T[keyof T] | null
50+
>x ? x[k] : null : T[keyof T] | null
51+
>x : T | null
52+
>x[k] : T[keyof T]
53+
>x : T
54+
>k : keyof T
55+
56+
return result; // T[keyof T] | null
57+
>result : T[keyof T] | null
58+
}
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
function foo<T extends object>(a: T[keyof T]) {
5+
let b: number = a; // Error
6+
}
7+
8+
// Repro from #54522
9+
10+
export function methodFnLength<T extends {}, K extends keyof T>(obj: T, methodKey: K): number {
11+
const fn = obj[methodKey];
12+
if (typeof fn !== 'function') {
13+
return 0;
14+
}
15+
return fn.length;
16+
}
17+
18+
// Repro from #54837
19+
20+
function getField<T extends object>(x: T | null, k: keyof T) {
21+
const result = x ? x[k] : null;
22+
return result; // T[keyof T] | null
23+
}

0 commit comments

Comments
 (0)