1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(10,11): error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
Type '{ a: `get${K}`; }' is not assignable to type '{ a: K; }'.
Types of property 'a' are incompatible.
Type '`get${K}`' is not assignable to type 'K'.
'`get${K}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Type '`get${string}`' is not assignable to type 'K'.
'`get${string}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
Type '{ a: Uppercase<K>; }' is not assignable to type '{ a: K; }'.
Types of property 'a' are incompatible.
Type 'Uppercase<K>' is not assignable to type 'K'.
'Uppercase<K>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Type 'Uppercase<string>' is not assignable to type 'K'.
'Uppercase<string>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
Type 'string' is not assignable to type 'K'.
'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts(25,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
'Foo<T>[`get${T}`]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
Type '`get${T}`' is not assignable to type 'T'.
'`get${T}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
Type '`get${string}`' is not assignable to type 'T'.
'`get${string}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
==== tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts (3 errors) ====
type Mapped1<K extends string> = { [P in K]: { a: P } };
function f1<K extends string>(obj: Mapped1<K>, key: K) {
const x: { a: K } = obj[key];
}
type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
const x: { a: K } = obj[key]; // Error
~
!!! error TS2322: Type 'Mapped2<K>[`get${K}`]' is not assignable to type '{ a: K; }'.
!!! error TS2322: Type '{ a: `get${K}`; }' is not assignable to type '{ a: K; }'.
!!! error TS2322: Types of property 'a' are incompatible.
!!! error TS2322: Type '`get${K}`' is not assignable to type 'K'.
!!! error TS2322: '`get${K}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type '`get${string}`' is not assignable to type 'K'.
!!! error TS2322: '`get${string}`' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
}
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
const x: { a: K } = obj[key]; // Error
~
!!! error TS2322: Type 'Mapped3<K>[Uppercase<K>]' is not assignable to type '{ a: K; }'.
!!! error TS2322: Type '{ a: Uppercase<K>; }' is not assignable to type '{ a: K; }'.
!!! error TS2322: Types of property 'a' are incompatible.
!!! error TS2322: Type 'Uppercase<K>' is not assignable to type 'K'.
!!! error TS2322: 'Uppercase<K>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type 'Uppercase<string>' is not assignable to type 'K'.
!!! error TS2322: 'Uppercase<string>' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type 'string' is not assignable to type 'K'.
!!! error TS2322: 'string' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint 'string'.
}
// Repro from #47794
type Foo<T extends string> = {
[RemappedT in T as `get${RemappedT}`]: RemappedT;
};
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'
~~~~~~~~~~~~~~
!!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
!!! 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'.
!!! error TS2322: Type '`get${T}`' is not assignable to type 'T'.
!!! error TS2322: '`get${T}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
!!! error TS2322: Type '`get${string}`' is not assignable to type 'T'.
!!! error TS2322: '`get${string}`' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
// Repro from #48626
interface Bounds {
min: number;
max: number;
}
type NumericBoundsOf<T> = {
[K in keyof T as T[K] extends number | undefined ? K : never]: Bounds;
}
function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>) {
for (const [key, val] of Object.entries(obj)) {
const boundsForKey = bounds[key as keyof NumericBoundsOf<T>];
if (boundsForKey) {
const { min, max } = boundsForKey;
if (min > val || max < val) return false;
}
}
return true;
}
// repro from #50030
type ObjectWithUnderscoredKeys<K extends string> = {
[k in K as `_${k}`]: true;
};
function genericTest<K extends string>(objectWithUnderscoredKeys: ObjectWithUnderscoredKeys<K>, key: K) {
const shouldBeTrue: true = objectWithUnderscoredKeys[`_${key}`];
}
|