Skip to content

Commit f382a68

Browse files
committed
Revert "Always substitute indexed generic mapped type when getting constraint from indexed access (microsoft#53066)"
This reverts commit abb4052.
1 parent 1a421a7 commit f382a68

File tree

6 files changed

+9
-128
lines changed

6 files changed

+9
-128
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14230,7 +14230,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1423014230
}
1423114231

1423214232
function getConstraintFromIndexedAccess(type: IndexedAccessType) {
14233-
if (isMappedTypeGenericIndexedAccess(type) || isGenericMappedType(type.objectType)) {
14233+
if (isMappedTypeGenericIndexedAccess(type)) {
1423414234
// For indexed access types of the form { [P in K]: E }[X], where K is non-generic and X is generic,
1423514235
// we substitute an instantiation of E where P is replaced with X.
1423614236
return substituteIndexedMappedType(type.objectType as MappedType, type.indexType);
Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
mappedTypeConstraints2.ts(10,11): error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
2-
Type '{ a: `get${K}`; }' is not assignable to type '{ a: K; }'.
3-
Types of property 'a' are incompatible.
4-
Type '`get${K}`' is not assignable to type 'K'.
5-
'`get${K}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
6-
Type '`get${string}`' is not assignable to type 'K'.
7-
'`get${string}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
2+
Type 'Mapped2<K>[`get${string}`]' is not assignable to type '{ a: K; }'.
83
mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
9-
Type '{ a: Uppercase<K>; }' is not assignable to type '{ a: K; }'.
10-
Types of property 'a' are incompatible.
11-
Type 'Uppercase<K>' is not assignable to type 'K'.
12-
'Uppercase<K>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
13-
Type 'Uppercase<string>' is not assignable to type 'K'.
14-
'Uppercase<string>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
15-
Type 'string' is not assignable to type 'K'.
16-
'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
4+
Type 'Mapped3<K>[Uppercase<string>]' is not assignable to type '{ a: K; }'.
5+
Type 'Mapped3<K>[string]' is not assignable to type '{ a: K; }'.
176
mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
18-
'Foo<T>[`get${T}`]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
19-
Type '`get${T}`' is not assignable to type 'T'.
20-
'`get${T}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
21-
Type '`get${string}`' is not assignable to type 'T'.
22-
'`get${string}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
7+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
238

249

2510
==== mappedTypeConstraints2.ts (3 errors) ====
@@ -35,12 +20,7 @@ mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not
3520
const x: { a: K } = obj[key]; // Error
3621
~
3722
!!! error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
38-
!!! error TS2322: Type '{ a: `get${K}`; }' is not assignable to type '{ a: K; }'.
39-
!!! error TS2322: Types of property 'a' are incompatible.
40-
!!! error TS2322: Type '`get${K}`' is not assignable to type 'K'.
41-
!!! error TS2322: '`get${K}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
42-
!!! error TS2322: Type '`get${string}`' is not assignable to type 'K'.
43-
!!! error TS2322: '`get${string}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
23+
!!! error TS2322: Type 'Mapped2<K>[`get${string}`]' is not assignable to type '{ a: K; }'.
4424
}
4525

4626
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
@@ -49,14 +29,8 @@ mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not
4929
const x: { a: K } = obj[key]; // Error
5030
~
5131
!!! error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
52-
!!! error TS2322: Type '{ a: Uppercase<K>; }' is not assignable to type '{ a: K; }'.
53-
!!! error TS2322: Types of property 'a' are incompatible.
54-
!!! error TS2322: Type 'Uppercase<K>' is not assignable to type 'K'.
55-
!!! error TS2322: 'Uppercase<K>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
56-
!!! error TS2322: Type 'Uppercase<string>' is not assignable to type 'K'.
57-
!!! error TS2322: 'Uppercase<string>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
58-
!!! error TS2322: Type 'string' is not assignable to type 'K'.
59-
!!! error TS2322: 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
32+
!!! error TS2322: Type 'Mapped3<K>[Uppercase<string>]' is not assignable to type '{ a: K; }'.
33+
!!! error TS2322: Type 'Mapped3<K>[string]' is not assignable to type '{ a: K; }'.
6034
}
6135

6236
// Repro from #47794
@@ -68,11 +42,7 @@ mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not
6842
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
6943
~~~~~~~~~~~~~~
7044
!!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
71-
!!! error TS2322: 'Foo<T>[`get${T}`]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
72-
!!! error TS2322: Type '`get${T}`' is not assignable to type 'T'.
73-
!!! error TS2322: '`get${T}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
74-
!!! error TS2322: Type '`get${string}`' is not assignable to type 'T'.
75-
!!! error TS2322: '`get${string}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
45+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
7646

7747
// Repro from #48626
7848

@@ -95,14 +65,4 @@ mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not
9565
}
9666
return true;
9767
}
98-
99-
// repro from #50030
100-
101-
type ObjectWithUnderscoredKeys<K extends string> = {
102-
[k in K as `_${k}`]: true;
103-
};
104-
105-
function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) {
106-
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
107-
}
10868

tests/baselines/reference/mappedTypeConstraints2.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>) {
4848
}
4949
return true;
5050
}
51-
52-
// repro from #50030
53-
54-
type ObjectWithUnderscoredKeys<K extends string> = {
55-
[k in K as `_${k}`]: true;
56-
};
57-
58-
function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) {
59-
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
60-
}
6151

6252

6353
//// [mappedTypeConstraints2.js]
@@ -83,9 +73,6 @@ function validate(obj, bounds) {
8373
}
8474
return true;
8575
}
86-
function genericTest(objectWithUnderscoredKeys, key) {
87-
const shouldBeTrue = objectWithUnderscoredKeys[`_${key}`];
88-
}
8976

9077

9178
//// [mappedTypeConstraints2.d.ts]
@@ -119,7 +106,3 @@ type NumericBoundsOf<T> = {
119106
[K in keyof T as T[K] extends number | undefined ? K : never]: Bounds;
120107
};
121108
declare function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>): boolean;
122-
type ObjectWithUnderscoredKeys<K extends string> = {
123-
[k in K as `_${k}`]: true;
124-
};
125-
declare function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K): void;

tests/baselines/reference/mappedTypeConstraints2.symbols

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -173,31 +173,3 @@ function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>) {
173173
return true;
174174
}
175175

176-
// repro from #50030
177-
178-
type ObjectWithUnderscoredKeys<K extends string> = {
179-
>ObjectWithUnderscoredKeys : Symbol(ObjectWithUnderscoredKeys, Decl(mappedTypeConstraints2.ts, 46, 1))
180-
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 50, 31))
181-
182-
[k in K as `_${k}`]: true;
183-
>k : Symbol(k, Decl(mappedTypeConstraints2.ts, 51, 5))
184-
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 50, 31))
185-
>k : Symbol(k, Decl(mappedTypeConstraints2.ts, 51, 5))
186-
187-
};
188-
189-
function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) {
190-
>genericTest : Symbol(genericTest, Decl(mappedTypeConstraints2.ts, 52, 2))
191-
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 54, 21))
192-
>objectWithUnderscoredKeys : Symbol(objectWithUnderscoredKeys, Decl(mappedTypeConstraints2.ts, 54, 39))
193-
>ObjectWithUnderscoredKeys : Symbol(ObjectWithUnderscoredKeys, Decl(mappedTypeConstraints2.ts, 46, 1))
194-
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 54, 21))
195-
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 54, 95))
196-
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 54, 21))
197-
198-
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
199-
>shouldBeTrue : Symbol(shouldBeTrue, Decl(mappedTypeConstraints2.ts, 55, 7))
200-
>objectWithUnderscoredKeys : Symbol(objectWithUnderscoredKeys, Decl(mappedTypeConstraints2.ts, 54, 39))
201-
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 54, 95))
202-
}
203-

tests/baselines/reference/mappedTypeConstraints2.types

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,3 @@ function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>) {
130130
>true : true
131131
}
132132

133-
// repro from #50030
134-
135-
type ObjectWithUnderscoredKeys<K extends string> = {
136-
>ObjectWithUnderscoredKeys : ObjectWithUnderscoredKeys<K>
137-
138-
[k in K as `_${k}`]: true;
139-
>true : true
140-
141-
};
142-
143-
function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) {
144-
>genericTest : <K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) => void
145-
>objectWithUnderscoredKeys : ObjectWithUnderscoredKeys<K>
146-
>key : K
147-
148-
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
149-
>shouldBeTrue : true
150-
>true : true
151-
>objectWithUnderscoredKeys[`_${key}`] : ObjectWithUnderscoredKeys<K>[`_${K}`]
152-
>objectWithUnderscoredKeys : ObjectWithUnderscoredKeys<K>
153-
>`_${key}` : `_${K}`
154-
>key : K
155-
}
156-

tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,3 @@ function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>) {
4949
}
5050
return true;
5151
}
52-
53-
// repro from #50030
54-
55-
type ObjectWithUnderscoredKeys<K extends string> = {
56-
[k in K as `_${k}`]: true;
57-
};
58-
59-
function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) {
60-
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
61-
}

0 commit comments

Comments
 (0)