Literal Types

Literals are exact values that are JavaScript primitives.

String Literals

You can use a string literal as a type. For example:

let foo: 'Hello';

Here we have created a variable called foo that will only allow the literal value 'Hello' to be assigned to it. This is demonstrated below:

let foo: 'Hello';
foo = 'Bar'; // Error: "Bar" is not assignable to type "Hello"

They are not very useful on their own but can be combined in a type union to create a powerful (and useful) abstraction e.g.:

type CardinalDirection =
    | "North"
    | "East"
    | "South"
    | "West";

function move(distance: number, direction: CardinalDirection) {
    // ...
}

move(1,"North"); // Okay
move(1,"Nurth"); // Error!

Other literal types

TypeScript also supports boolean and number literal types, e.g.:

Inference

Quite commonly you get an error like Type string is not assignable to type "foo". The following example demonstrates this.

This is because test is inferred to be of type {someProp: string}. The fix here is to use a simple type assertion to tell TypeScript the literal you want it to infer as shown below:

or use a type annotation that helps TypeScript infer the correct thing at the point of declaration:

Use cases

Valid use cases for string literal types are:

String based enums

TypeScript enums are number based. You can use string literals with union types to mock a string based enum as we did in the CardinalDirection example above. You can even generate a Key:Value structure using the following function:

And then generate the literal type union using keyof typeof. Here is a complete example:

Modelling existing JavaScript APIs

E.g. CodeMirror editor has an option readOnly that can either be a boolean or the literal string "nocursor" (effective valid values true,false,"nocursor"). It can be declared as:

Discriminated Unions

We will cover this later in the book.

Last updated

Was this helpful?