TypeScript Literal Types
A literal type names a specific allowed value. Combine literals into a small vocabulary when arbitrary text would allow misspelled states or unsupported options.
How it works
A packet request can be queued, ready or collected. These names are part of the program’s contract, so the checker should reject a fourth spelling. A union of string literals works well for readable state values and JSON-friendly records. It still does not check data loaded from storage; a boundary parser must confirm that incoming text belongs to this vocabulary.
Run the code
Try in playground loads this source without running it. Choose Run when ready. The existing TypeScript 5.8.3 worker transpiles and executes one file; it does not perform full semantic type checking, load npm imports or provide DOM/Node APIs. Use the local compiler for the type contract.
For a full check, save the three downloads together in a new folder, run npm install, then npm run check. Use npm run build followed by npm start to execute the checked JavaScript with Node. The supplied project enables strict checking, indexed-access checks and exact optional properties.
Example
type RequestState = "queued" | "ready" | "collected";
function isFinished(state: RequestState): boolean {
return state === "collected";
}
const state: RequestState = "ready";
console.log(state, isFinished(state));
Expected result
ready false
The state annotation permits only the three listed strings. The function’s comparison has a clear domain and returns a boolean. Adding a state later is an intentional model change; do not store human-facing sentences as state names, because translation and wording changes would then alter the data contract.