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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
=== tests/cases/conformance/types/mapped/mappedTypeConstraints2.ts ===
type Mapped1<K extends string> = { [P in K]: { a: P } };
>Mapped1 : Symbol(Mapped1, Decl(mappedTypeConstraints2.ts, 0, 0))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 0, 13))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 0, 36))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 0, 13))
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 0, 46))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 0, 36))
function f1<K extends string>(obj: Mapped1<K>, key: K) {
>f1 : Symbol(f1, Decl(mappedTypeConstraints2.ts, 0, 56))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 2, 30))
>Mapped1 : Symbol(Mapped1, Decl(mappedTypeConstraints2.ts, 0, 0))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 2, 46))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
const x: { a: K } = obj[key];
>x : Symbol(x, Decl(mappedTypeConstraints2.ts, 3, 9))
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 3, 14))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 2, 12))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 2, 30))
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 2, 46))
}
type Mapped2<K extends string> = { [P in K as `get${P}`]: { a: P } };
>Mapped2 : Symbol(Mapped2, Decl(mappedTypeConstraints2.ts, 4, 1))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 6, 13))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 6, 36))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 6, 13))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 6, 36))
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 6, 59))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 6, 36))
function f2<K extends string>(obj: Mapped2<K>, key: `get${K}`) {
>f2 : Symbol(f2, Decl(mappedTypeConstraints2.ts, 6, 69))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 8, 30))
>Mapped2 : Symbol(Mapped2, Decl(mappedTypeConstraints2.ts, 4, 1))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 8, 46))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
const x: { a: K } = obj[key]; // Error
>x : Symbol(x, Decl(mappedTypeConstraints2.ts, 9, 9))
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 9, 14))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 8, 12))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 8, 30))
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 8, 46))
}
type Mapped3<K extends string> = { [P in K as Uppercase<P>]: { a: P } };
>Mapped3 : Symbol(Mapped3, Decl(mappedTypeConstraints2.ts, 10, 1))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 12, 13))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 12, 36))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 12, 13))
>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 12, 36))
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 12, 62))
>P : Symbol(P, Decl(mappedTypeConstraints2.ts, 12, 36))
function f3<K extends string>(obj: Mapped3<K>, key: Uppercase<K>) {
>f3 : Symbol(f3, Decl(mappedTypeConstraints2.ts, 12, 72))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 14, 30))
>Mapped3 : Symbol(Mapped3, Decl(mappedTypeConstraints2.ts, 10, 1))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 14, 46))
>Uppercase : Symbol(Uppercase, Decl(lib.es5.d.ts, --, --))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
const x: { a: K } = obj[key]; // Error
>x : Symbol(x, Decl(mappedTypeConstraints2.ts, 15, 9))
>a : Symbol(a, Decl(mappedTypeConstraints2.ts, 15, 14))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 14, 12))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 14, 30))
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 14, 46))
}
// Repro from #47794
type Foo<T extends string> = {
>Foo : Symbol(Foo, Decl(mappedTypeConstraints2.ts, 16, 1))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 20, 9))
[RemappedT in T as `get${RemappedT}`]: RemappedT;
>RemappedT : Symbol(RemappedT, Decl(mappedTypeConstraints2.ts, 21, 5))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 20, 9))
>RemappedT : Symbol(RemappedT, Decl(mappedTypeConstraints2.ts, 21, 5))
>RemappedT : Symbol(RemappedT, Decl(mappedTypeConstraints2.ts, 21, 5))
};
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'
>get : Symbol(get, Decl(mappedTypeConstraints2.ts, 24, 5))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
>t : Symbol(t, Decl(mappedTypeConstraints2.ts, 24, 31))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
>foo : Symbol(foo, Decl(mappedTypeConstraints2.ts, 24, 36))
>Foo : Symbol(Foo, Decl(mappedTypeConstraints2.ts, 16, 1))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 24, 13))
>foo : Symbol(foo, Decl(mappedTypeConstraints2.ts, 24, 36))
>t : Symbol(t, Decl(mappedTypeConstraints2.ts, 24, 31))
// Repro from #48626
interface Bounds {
>Bounds : Symbol(Bounds, Decl(mappedTypeConstraints2.ts, 24, 71))
min: number;
>min : Symbol(Bounds.min, Decl(mappedTypeConstraints2.ts, 28, 18))
max: number;
>max : Symbol(Bounds.max, Decl(mappedTypeConstraints2.ts, 29, 16))
}
type NumericBoundsOf<T> = {
>NumericBoundsOf : Symbol(NumericBoundsOf, Decl(mappedTypeConstraints2.ts, 31, 1))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 33, 21))
[K in keyof T as T[K] extends number | undefined ? K : never]: Bounds;
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 34, 5))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 33, 21))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 33, 21))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 34, 5))
>K : Symbol(K, Decl(mappedTypeConstraints2.ts, 34, 5))
>Bounds : Symbol(Bounds, Decl(mappedTypeConstraints2.ts, 24, 71))
}
function validate<T extends object>(obj: T, bounds: NumericBoundsOf<T>) {
>validate : Symbol(validate, Decl(mappedTypeConstraints2.ts, 35, 1))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 37, 18))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 37, 36))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 37, 18))
>bounds : Symbol(bounds, Decl(mappedTypeConstraints2.ts, 37, 43))
>NumericBoundsOf : Symbol(NumericBoundsOf, Decl(mappedTypeConstraints2.ts, 31, 1))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 37, 18))
for (const [key, val] of Object.entries(obj)) {
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 38, 16))
>val : Symbol(val, Decl(mappedTypeConstraints2.ts, 38, 20))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>obj : Symbol(obj, Decl(mappedTypeConstraints2.ts, 37, 36))
const boundsForKey = bounds[key as keyof NumericBoundsOf<T>];
>boundsForKey : Symbol(boundsForKey, Decl(mappedTypeConstraints2.ts, 39, 13))
>bounds : Symbol(bounds, Decl(mappedTypeConstraints2.ts, 37, 43))
>key : Symbol(key, Decl(mappedTypeConstraints2.ts, 38, 16))
>NumericBoundsOf : Symbol(NumericBoundsOf, Decl(mappedTypeConstraints2.ts, 31, 1))
>T : Symbol(T, Decl(mappedTypeConstraints2.ts, 37, 18))
if (boundsForKey) {
>boundsForKey : Symbol(boundsForKey, Decl(mappedTypeConstraints2.ts, 39, 13))
const { min, max } = boundsForKey;
>min : Symbol(min, Decl(mappedTypeConstraints2.ts, 41, 19))
>max : Symbol(max, Decl(mappedTypeConstraints2.ts, 41, 24))
>boundsForKey : Symbol(boundsForKey, Decl(mappedTypeConstraints2.ts, 39, 13))
if (min > val || max < val) return false;
>min : Symbol(min, Decl(mappedTypeConstraints2.ts, 41, 19))
>val : Symbol(val, Decl(mappedTypeConstraints2.ts, 38, 20))
>max : Symbol(max, Decl(mappedTypeConstraints2.ts, 41, 24))
>val : Symbol(val, Decl(mappedTypeConstraints2.ts, 38, 20))
}
}
return true;
}
|