liveBook · Manning

Literal type

As explained in Essential TypeScript 5, Third Edition

A literal string type is a TypeScript type whose values are restricted to one or more specific string literals.

A literal type restricts a value to specific choices. For strings, `"London" | "Paris" | "Chicago"` accepts those three values rather than any string. The `|` syntax resembles a type union, but its members are values instead of general types. section 9.5 section 9.5.4

Restrict a function argument

In `src/index.ts`, a parameter can accept only the quantities the function handles: `function calculatePrice(quantity: 1 | 2, price: number): number {` ` return quantity * price;` `}` `calculatePrice(2, 19.99); // 39.98` A different number is a compiler error. The same rule applies to string literal parameters: `getCityString("London")` accepts an allowed city name. section 9.5.1 section 9.5.4

Each set of allowed values is a distinct type. Assignment depends on the value the compiler can establish at that point: a value known to be `1` fits `1 | 2 | 3`, while one known to be `100` does not. Literal types can also mix numbers, strings, booleans, and enum values; `string | true | 3` permits any string but only the specified boolean and number. section 9.5 section 9.5.2

Build a constrained string

A template literal type can describe complete strings assembled from allowed parts: `function getCityString(city: "London" | "Paris" | "Chicago")` ` : `City: ${"London" | "Paris" | "Chicago"}` {` ` return `City: ${city}`;` `}` The result type permits `"City: London"`, `"City: Paris"`, and `"City: Chicago"`. section 9.5.4

Literal types also work with function overloads, which can relate particular inputs to particular results. In generic code, a string literal can identify a property type: `getValue<Product, "name">(p, "name")` uses the first `"name"` for type checking and the second as the runtime argument. Mapped and conditional types can produce unions of eligible property names, such as `"price"` for a numeric property. section 9.5.3 section 13.4.2 section 13.6.5

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Works through assignment errors, function signatures, mixed values, overloads, and template expansion; does not detail compiler internals.
Examples
Uses restricted quantities, city-name strings, expanded city labels, and numeric property names.
What it assumes
Assumes familiarity with TypeScript type annotations, unions, and JavaScript functions; introduces literal-value restrictions through examples.
What you can do after
Apply it in TypeScript to reject unsupported values and express constrained function inputs, outputs, and strings.
How it connects
Connects type unions, enums, function overloads, template literal types, generic type parameters, and mapped types.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.