Mapped types — building one object type from another¶
Level: 301 · for anyone who has used Partial or Readonly
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: { [K in keyof T]: T[K] } copies each property's readonly and ? along with its type, while the same loop over a plain union of key names copies neither; -readonly and -? strip them.
Keywords: mapped type, key remapping, -readonly, -?
What the finished page will answer¶
- Why does
{ [K in keyof T]: T[K] }keepreadonlyand?, while{ [K in "x" | "y"]: Point[K] }drops both? - What do
-readonlyand-?remove, and what does+?add? - How does
asrename keys, as in`get${Capitalize<string & K>}`, and how doesas neverdrop a key? - What does a mapped type do to an array or a tuple — is
Stringify<[number, boolean]>a tuple or an object? - How deep does a mapped type go — is
Readonly<T>readonly all the way down?
Examples it will need¶
-
mapped_types_modifiers_tserror.ts— the errors that show which mapped types kept readonly and ? and which dropped them -
mapped_types_getters_ts.ts— an object of getX and getY functions built to a key-remapped type, and what each returns
See also¶
- Utility types — the standard library's mapped types
keyofandtypeofin types — keyof T, the key set a mapped type walks- Template literal types — building new key names with as
- Object types — the readonly and optional modifiers being copied