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
