0Pricing
TypeScript Academy · Lesson

Key Remapping with as in Mapped Types

Rename keys during type transformation.

Key Remapping with as in Mapped Types is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

TypeScript 4.1 added the `as` clause in mapped types, allowing you to remap (rename) keys as part of the type transformation.

Basic Key Remapping

Add `as NewKeyType` after the key variable to remap to a new key name.
type Prefixed<T> = {
  [K in keyof T as `prefix_${string & K}`]: T[K]
};
type P = Prefixed<{ name: string }>;
// { prefix_name: string }

Filtering Keys with never

Return never from the `as` clause to remove that key from the resulting type.
type OnlyStrings<T> = {
  [K in keyof T as T[K] extends string ? K : never]: T[K]
};

Generating Getter Names

Use Capitalize with template literal types to generate getter method names.
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K]
};
type UserGetters = Getters<{ name: string; age: number }>;
// { getName: () => string; getAge: () => number }

Generating Setter Names

Similarly generate setter names for a type's properties.
type Setters<T> = {
  [K in keyof T as `set${Capitalize<string & K>}`]: (val: T[K]) => void
};

Invert a Record Type

Swap keys and values using key remapping.
type Invert<T extends Record<string, string>> = {
  [K in keyof T as T[K]]: K
};

Conditional Key Remapping

Apply different key transformations based on value type.
type MethodKeys<T> = {
  [K in keyof T as T[K] extends Function ? K : never]: T[K]
};

Combining with Template Literals

Use template literal types in the remapping to generate namespaced keys.
type Namespaced<T, NS extends string> = {
  [K in keyof T as `${NS}::${string & K}`]: T[K]
};

Mapping Symbol Keys

With key remapping you can also work with symbol keys.

Practical: FormData Type

Derive a string-value form object from a typed data model.
type FormFields<T> = {
  [K in keyof T as K extends string ? K : never]: string
};

Combining as with Conditional Types

Key remapping and conditional types compose to create highly expressive type transformations.

Quick Check

What does returning `never` from the `as` clause in a mapped type do to that key?

Recap

The `as` clause in mapped types lets you rename, filter (via never), and transform keys using template literals and conditionals. It enables powerful patterns like getter/setter generation and property filtering by value type.

Frequently asked questions

Is the “Key Remapping with as in Mapped Types” lesson free?

Yes — the full text of “Key Remapping with as in Mapped Types” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Key Remapping with as in Mapped Types”?

Rename keys during type transformation. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Key Remapping with as in Mapped Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Mapped Types: Transforming Object Types
  2. Modifiers in Mapped Types: readonly and ?
  3. Template Literal Types: String Patterns
  4. Key Remapping with as in Mapped Types
← Back to TypeScript Academy