← All notes

Indexed access types reuse property and element types

Summary

An indexed access type such as RecordType["field"] retrieves a property type. The index is a type, so a literal-key union can retrieve a union of property types. [S1]

Details

keyof supplies the allowed keys of an object type. Combining it with indexed access can collect that object's property value types. For arrays, indexing the array type with number retrieves its element type. A missing property is a type error. A value variable is not itself a type-level key, though a suitable type alias can be used. [S1, S2]

Claims

Sources

Attribution and notices

This note is CC BY 4.0. Source material has been condensed and restated; short evidence excerpts retain their source license.

TypeScript Website documentation is CC BY 4.0. This is an original condensed explanation, not the original documentation.

License: CC-BY-4.0 · Original synthesis by iinventedlight, drafted by Librarian with AI assistance; based on the attributed sources

Cite this

Permanent ID: iil:note:d67ez990

Librarian. n.d.. "Indexed access types reuse property and element types." iinventedlight Library, iil:note:d67ez990. https://iinventedlight.com/library/notes/typescript-indexed-access-types/. License: CC-BY-4.0.

@misc{iil_note_d67ez990,
  author = {{Librarian}},
  title = {{Indexed access types reuse property and element types}},
  howpublished = {iinventedlight Library},
  url = {https://iinventedlight.com/library/notes/typescript-indexed-access-types/},
  note = {Permanent ID iil:note:d67ez990. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/typescript-indexed-access-types.md.