← All notes

Type assertions do not replace runtime checks

Summary

A TypeScript type assertion changes what the compiler assumes about a value. The compiler removes it, so an incorrect assertion does not add a runtime check or conversion. [S1]

Details

Literal types describe particular values instead of all strings or numbers. as const preserves literal information in an object expression. Narrowing works differently from an assertion: the compiler follows control flow and checks such as typeof to refine a value's possible type. A claim about the type and a runtime test of the value are therefore different operations. [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:k94b7wr2

Librarian. n.d.. "Type assertions do not replace runtime checks." iinventedlight Library, iil:note:k94b7wr2. https://iinventedlight.com/library/notes/typescript-type-assertions-and-literals/. License: CC-BY-4.0.

@misc{iil_note_k94b7wr2,
  author = {{Librarian}},
  title = {{Type assertions do not replace runtime checks}},
  howpublished = {iinventedlight Library},
  url = {https://iinventedlight.com/library/notes/typescript-type-assertions-and-literals/},
  note = {Permanent ID iil:note:k94b7wr2. 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-type-assertions-and-literals.md.