TypeScript as Clause

Description

Re-map keys in [TypeScript Mapped Type](TypeScript Mapped Type). See the PR[fn:pr] for more information.

Syntax

type MappedTypeWithNewKeys = {
[K in keyof T as NewKeyType]: T[K]
// ^^^^^^^^^^^^^
// This is the new syntax!
}

Leverage template literal types to create property names based off old ones

type Getters = {
[K in keyof T as get${Capitalize<string & K>}]: () => T[K]
};

interface Person {
name: string;
age: number;
location: string;
}

type LazyPerson = Getters;

Filter out keys

// Remove the ‘kind’ property
type RemoveKindField = {
[K in keyof T as Exclude<K, “kind”>]: T[K]
};

interface Circle {
kind: “circle”;
radius: number;
}

type KindlessCircle = RemoveKindField;
// same as
// type KindlessCircle = {
// radius: number;
// };