Sub topic 3
Template literal types for string keys
This chapter assumes you already know mapped types, keyof, conditional types, and infer from the earlier chapters.
Template literal types
A template literal type builds a string type from other string types. Its syntax matches JavaScript template strings. When you give it a union, it produces every combination:
type Lang = "en" | "fr";
type Key = `title_${Lang}`; // "title_en" | "title_fr"
type Upper = Capitalize<"name">; // "Name"Renaming and filtering keys with as
An as clause inside a mapped type renames each key. keyof T can include number and symbol keys, so you need to intersect it with string before using it in a template:
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type UserGetters = Getters<{ id: number; name: string }>;
// { getId: () => number; getName: () => string }If a key is remapped to never, that property is dropped. You can use this to filter properties by their value type:
type PickByValue<T, V> = {
[K in keyof T as T[K] extends V ? K : never]: T[K];
};
type S = PickByValue<{ id: number; name: string }, string>; // { name: string }Recursive types
A type alias can refer to itself. That lets it unwrap nested promises or apply readonly at every depth of an object:
type MyAwaited<T> = T extends Promise<infer U> ? MyAwaited<U> : T;
type P = MyAwaited<Promise<Promise<number>>>; // number
type DeepReadonly<T> = T extends (...args: any[]) => any
? T
: T extends object
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: T;DeepReadonly checks for functions first. Functions are objects, so without that check they would be turned into mapped types.
Edge cases: never, any, and non-distributive checks
| Input to `T extends string ? true : false` | Result | Why | Column |
|---|---|---|---|
| `never` | `never` | `never` is an empty union, so there is nothing to distribute over | |
| `any` | `boolean` | TypeScript evaluates both branches and returns their union | |
| `"a" \ | 1` | `boolean` | The check distributes over each union member |
To test for never itself, wrap both sides in a tuple so the check does not distribute:
type IsNever<T> = [T] extends [never] ? true : false;
type X = IsNever<never>; // truePutting it together: event maps
This type combines key remapping, a template literal, and indexed access. It produces handler names and parameter types that stay in sync with the form type:
type ChangeHandlers<T> = {
[K in keyof T & string as `on${Capitalize<K>}Change`]: (value: T[K]) => void;
};
type FormHandlers = ChangeHandlers<{ email: string; age: number }>;
// { onEmailChange: (value: string) => void; onAgeChange: (value: number) => void }Exercise
Write a type Setters<T> that turns { id: number } into { setId: (value: number) => void }, keeping only the keys of T that are strings.
Show answer
type Setters<T> = { [K in keyof T & string as set${Capitalize<K>}]: (value: T[K]) => void };
Exercise
What does IsString<never> resolve to when IsString<T> = T extends string ? true : false, and how would you make it return false instead?
Show answer
It resolves to never, because the check distributes over an empty union. Writing [T] extends [string] ? true : false turns distribution off. With that version the result is true, because never is assignable to string. To get false, check for never first: [T] extends [never] ? false : (T extends string ? true : false).