Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Literal Types | Working with Complex and Composed Types
TypeScript Types Fundamentals

bookLiteral Types

Literal types in TypeScript allow you to specify that a variable can only have one or more exact values, rather than any value of a broader type like string or number. This restriction helps prevent errors by ensuring that only expected values are used in your code.

1234567891011
// String literal type let direction: "left" | "right" | "up" | "down"; direction = "left"; // OK direction = "up"; // OK // direction = "forward"; // Error: Type '"forward"' is not assignable to type '"left" | "right" | "up" | "down"' // Number literal type let diceRoll: 1 | 2 | 3 | 4 | 5 | 6; diceRoll = 4; // OK diceRoll = 6; // OK // diceRoll = 7; // Error: Type '7' is not assignable to type '1 | 2 | 3 | 4 | 5 | 6'
copy

In the example above, the direction variable is limited to four possible string values: "left", "right", "up", or "down". Any attempt to assign a different string will result in a type error. Similarly, the diceRoll variable can only be one of the numbers from 1 to 6, matching the faces of a standard die. Literal types are especially useful for function parameters, configuration options, or any situation where only a specific set of values makes sense. By using literal types, you make your code safer, clearer, and more predictable.

question mark

Which of the following declarations use literal types in TypeScript?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 8.33

bookLiteral Types

Deslize para mostrar o menu

Literal types in TypeScript allow you to specify that a variable can only have one or more exact values, rather than any value of a broader type like string or number. This restriction helps prevent errors by ensuring that only expected values are used in your code.

1234567891011
// String literal type let direction: "left" | "right" | "up" | "down"; direction = "left"; // OK direction = "up"; // OK // direction = "forward"; // Error: Type '"forward"' is not assignable to type '"left" | "right" | "up" | "down"' // Number literal type let diceRoll: 1 | 2 | 3 | 4 | 5 | 6; diceRoll = 4; // OK diceRoll = 6; // OK // diceRoll = 7; // Error: Type '7' is not assignable to type '1 | 2 | 3 | 4 | 5 | 6'
copy

In the example above, the direction variable is limited to four possible string values: "left", "right", "up", or "down". Any attempt to assign a different string will result in a type error. Similarly, the diceRoll variable can only be one of the numbers from 1 to 6, matching the faces of a standard die. Literal types are especially useful for function parameters, configuration options, or any situation where only a specific set of values makes sense. By using literal types, you make your code safer, clearer, and more predictable.

question mark

Which of the following declarations use literal types in TypeScript?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4
some-alt