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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
//// [mappedTypeAsClauses.ts]
// Mapped type 'as N' clauses
type Getters<T> = { [P in keyof T & string as `get${Capitalize<P>}`]: () => T[P] };
type TG1 = Getters<{ foo: string, bar: number, baz: { z: boolean } }>;
// Mapped type with 'as N' clause has no constraint on 'in T' clause
type PropDef<K extends keyof any, T> = { name: K, type: T };
type TypeFromDefs<T extends PropDef<keyof any, any>> = { [P in T as P['name']]: P['type'] };
type TP1 = TypeFromDefs<{ name: 'a', type: string } | { name: 'b', type: number } | { name: 'a', type: boolean }>;
// No array or tuple type mapping when 'as N' clause present
type TA1 = Getters<string[]>;
type TA2 = Getters<[number, boolean]>;
// Filtering using 'as N' clause
type Methods<T> = { [P in keyof T as T[P] extends Function ? P : never]: T[P] };
type TM1 = Methods<{ foo(): number, bar(x: string): boolean, baz: string | number }>;
// Mapping to multiple names using 'as N' clause
type DoubleProp<T> = { [P in keyof T & string as `${P}1` | `${P}2`]: T[P] }
type TD1 = DoubleProp<{ a: string, b: number }>; // { a1: string, a2: string, b1: number, b2: number }
type TD2 = keyof TD1; // 'a1' | 'a2' | 'b1' | 'b2'
type TD3<U> = keyof DoubleProp<U>; // `${keyof U & string}1` | `${keyof U & string}2`
// Repro from #40619
type Lazyify<T> = {
[K in keyof T as `get${Capitalize<K & string>}`]: () => T[K]
};
interface Person {
readonly name: string;
age: number;
location?: string;
}
type LazyPerson = Lazyify<Person>;
// Repro from #40833
type Example = {foo: string, bar: number};
type PickByValueType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K]
};
type T1 = PickByValueType<Example, string>;
const e1: T1 = {
foo: "hello"
};
type T2 = keyof T1;
const e2: T2 = "foo";
// Repro from #41133
interface Car {
name: string;
seats: number;
engine: Engine;
wheels: Wheel[];
}
interface Engine {
manufacturer: string;
horsepower: number;
}
interface Wheel {
type: "summer" | "winter";
radius: number;
}
type Primitive = string | number | boolean;
type OnlyPrimitives<T> = { [K in keyof T as T[K] extends Primitive ? K : never]: T[K] };
let primitiveCar: OnlyPrimitives<Car>; // { name: string; seats: number; }
let keys: keyof OnlyPrimitives<Car>; // "name" | "seats"
type KeysOfPrimitives<T> = keyof OnlyPrimitives<T>;
let carKeys: KeysOfPrimitives<Car>; // "name" | "seats"
// Repro from #41453
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type If<Cond extends boolean, Then, Else> = Cond extends true ? Then : Else;
type GetKey<S, V> = keyof { [TP in keyof S as Equal<S[TP], V> extends true ? TP : never]: any };
type GetKeyWithIf<S, V> = keyof { [TP in keyof S as If<Equal<S[TP], V>, TP, never>]: any };
type GetObjWithIf<S, V> = { [TP in keyof S as If<Equal<S[TP], V>, TP, never>]: any };
type Task = {
isDone: boolean;
};
type Schema = {
root: {
title: string;
task: Task;
}
Task: Task;
};
type Res1 = GetKey<Schema, Schema['root']['task']>; // "Task"
type Res2 = GetKeyWithIf<Schema, Schema['root']['task']>; // "Task"
type Res3 = keyof GetObjWithIf<Schema, Schema['root']['task']>; // "Task"
// Repro from #44019
type KeysExtendedBy<T, U> = keyof { [K in keyof T as U extends T[K] ? K : never] : T[K] };
interface M {
a: boolean;
b: number;
}
function f(x: KeysExtendedBy<M, number>) {
return x;
}
f("a"); // Error, should allow only "b"
type NameMap = { 'a': 'x', 'b': 'y', 'c': 'z' };
// Distributive, will be simplified
type TS0<T> = keyof { [P in keyof T as keyof Record<P, number>]: string };
type TS1<T> = keyof { [P in keyof T as Extract<P, 'a' | 'b' | 'c'>]: string };
type TS2<T> = keyof { [P in keyof T as P & ('a' | 'b' | 'c')]: string };
type TS3<T> = keyof { [P in keyof T as Exclude<P, 'a' | 'b' | 'c'>]: string };
type TS4<T> = keyof { [P in keyof T as NameMap[P & keyof NameMap]]: string };
type TS5<T> = keyof { [P in keyof T & keyof NameMap as NameMap[P]]: string };
type TS6<T, U, V> = keyof { [ K in keyof T as V & (K extends U ? K : never)]: string };
// Non-distributive, won't be simplified
type TN0<T> = keyof { [P in keyof T as T[P] extends number ? P : never]: string };
type TN1<T> = keyof { [P in keyof T as number extends T[P] ? P : never]: string };
type TN2<T> = keyof { [P in keyof T as 'a' extends P ? 'x' : 'y']: string };
type TN3<T> = keyof { [P in keyof T as Exclude<Exclude<Exclude<P, 'c'>, 'b'>, 'a'>]: string };
type TN4<T, U> = keyof { [K in keyof T as (K extends U ? T[K] : never) extends T[K] ? K : never]: string };
type TN5<T, U> = keyof { [K in keyof T as keyof { [P in K as T[P] extends U ? K : never]: true }]: string };
//// [mappedTypeAsClauses.js]
"use strict";
// Mapped type 'as N' clauses
var e1 = {
foo: "hello"
};
var e2 = "foo";
var primitiveCar; // { name: string; seats: number; }
var keys; // "name" | "seats"
var carKeys; // "name" | "seats"
function f(x) {
return x;
}
f("a"); // Error, should allow only "b"
//// [mappedTypeAsClauses.d.ts]
type Getters<T> = {
[P in keyof T & string as `get${Capitalize<P>}`]: () => T[P];
};
type TG1 = Getters<{
foo: string;
bar: number;
baz: {
z: boolean;
};
}>;
type PropDef<K extends keyof any, T> = {
name: K;
type: T;
};
type TypeFromDefs<T extends PropDef<keyof any, any>> = {
[P in T as P['name']]: P['type'];
};
type TP1 = TypeFromDefs<{
name: 'a';
type: string;
} | {
name: 'b';
type: number;
} | {
name: 'a';
type: boolean;
}>;
type TA1 = Getters<string[]>;
type TA2 = Getters<[number, boolean]>;
type Methods<T> = {
[P in keyof T as T[P] extends Function ? P : never]: T[P];
};
type TM1 = Methods<{
foo(): number;
bar(x: string): boolean;
baz: string | number;
}>;
type DoubleProp<T> = {
[P in keyof T & string as `${P}1` | `${P}2`]: T[P];
};
type TD1 = DoubleProp<{
a: string;
b: number;
}>;
type TD2 = keyof TD1;
type TD3<U> = keyof DoubleProp<U>;
type Lazyify<T> = {
[K in keyof T as `get${Capitalize<K & string>}`]: () => T[K];
};
interface Person {
readonly name: string;
age: number;
location?: string;
}
type LazyPerson = Lazyify<Person>;
type Example = {
foo: string;
bar: number;
};
type PickByValueType<T, U> = {
[K in keyof T as T[K] extends U ? K : never]: T[K];
};
type T1 = PickByValueType<Example, string>;
declare const e1: T1;
type T2 = keyof T1;
declare const e2: T2;
interface Car {
name: string;
seats: number;
engine: Engine;
wheels: Wheel[];
}
interface Engine {
manufacturer: string;
horsepower: number;
}
interface Wheel {
type: "summer" | "winter";
radius: number;
}
type Primitive = string | number | boolean;
type OnlyPrimitives<T> = {
[K in keyof T as T[K] extends Primitive ? K : never]: T[K];
};
declare let primitiveCar: OnlyPrimitives<Car>;
declare let keys: keyof OnlyPrimitives<Car>;
type KeysOfPrimitives<T> = keyof OnlyPrimitives<T>;
declare let carKeys: KeysOfPrimitives<Car>;
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type If<Cond extends boolean, Then, Else> = Cond extends true ? Then : Else;
type GetKey<S, V> = keyof {
[TP in keyof S as Equal<S[TP], V> extends true ? TP : never]: any;
};
type GetKeyWithIf<S, V> = keyof {
[TP in keyof S as If<Equal<S[TP], V>, TP, never>]: any;
};
type GetObjWithIf<S, V> = {
[TP in keyof S as If<Equal<S[TP], V>, TP, never>]: any;
};
type Task = {
isDone: boolean;
};
type Schema = {
root: {
title: string;
task: Task;
};
Task: Task;
};
type Res1 = GetKey<Schema, Schema['root']['task']>;
type Res2 = GetKeyWithIf<Schema, Schema['root']['task']>;
type Res3 = keyof GetObjWithIf<Schema, Schema['root']['task']>;
type KeysExtendedBy<T, U> = keyof {
[K in keyof T as U extends T[K] ? K : never]: T[K];
};
interface M {
a: boolean;
b: number;
}
declare function f(x: KeysExtendedBy<M, number>): "b";
type NameMap = {
'a': 'x';
'b': 'y';
'c': 'z';
};
type TS0<T> = keyof {
[P in keyof T as keyof Record<P, number>]: string;
};
type TS1<T> = keyof {
[P in keyof T as Extract<P, 'a' | 'b' | 'c'>]: string;
};
type TS2<T> = keyof {
[P in keyof T as P & ('a' | 'b' | 'c')]: string;
};
type TS3<T> = keyof {
[P in keyof T as Exclude<P, 'a' | 'b' | 'c'>]: string;
};
type TS4<T> = keyof {
[P in keyof T as NameMap[P & keyof NameMap]]: string;
};
type TS5<T> = keyof {
[P in keyof T & keyof NameMap as NameMap[P]]: string;
};
type TS6<T, U, V> = keyof {
[K in keyof T as V & (K extends U ? K : never)]: string;
};
type TN0<T> = keyof {
[P in keyof T as T[P] extends number ? P : never]: string;
};
type TN1<T> = keyof {
[P in keyof T as number extends T[P] ? P : never]: string;
};
type TN2<T> = keyof {
[P in keyof T as 'a' extends P ? 'x' : 'y']: string;
};
type TN3<T> = keyof {
[P in keyof T as Exclude<Exclude<Exclude<P, 'c'>, 'b'>, 'a'>]: string;
};
type TN4<T, U> = keyof {
[K in keyof T as (K extends U ? T[K] : never) extends T[K] ? K : never]: string;
};
type TN5<T, U> = keyof {
[K in keyof T as keyof {
[P in K as T[P] extends U ? K : never]: true;
}]: string;
};
|