TypeScript 7 reference · Type Operators and Keywords

TypeScript mapped types ({ [K in Keys]: … }) Operator

Create a new object type by transforming every property of another, the way Partial and Readonly are built.

TypeScript 7 Since 2.1

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

{ [K in keyof T]: NewType }

Examples

Same keys, new value typeTry it yourself
type Flags<T> = { [K in keyof T]: boolean };
interface Features {
  darkMode: string;
  beta: number;
}
const enabled: Flags<Features> = { darkMode: true, beta: false };
console.log(enabled);

Output

{ darkMode: true, beta: false }
Renaming keys with asTry it yourself
type Getters<T> = { [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K] };
interface Person {
  name: string;
  age: number;
}
const p: Getters<Person> = { getName: () => "Ana", getAge: () => 34 };
console.log(p.getName(), p.getAge());

Output

Ana 34

Version history

VersionChange
TypeScript 2.1Added

Was this page helpful?