10LOC

#template-literal-types

TypeScriptadvanced

Mapped types with key remapping via as

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

export const toGetters = <T extends object>(obj: T): Getters<T> => {

Turn a plain object type into a matching set of getter methods, renaming every key to getX without writing each one by hand.

TypeScriptadvanced

Template literal types for typed route strings

type ParamNames<Path extends string> = Path extends `${string}:${infer Param}/${infer Rest}`
  ? Param | ParamNames<Rest>
  : Path extends `${string}:${infer Param}`
    ? Param
    : never;

Extract :param names out of a route string at the type level, so a route's caller must supply exactly the params that route needs.