Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Special Types | Understanding TypeScript Types
TypeScript Types Fundamentals

bookSpecial Types

TypeScript provides several special types that help you describe unusual or edge-case values in your code. Understanding these types lets you write code that is both safer and more expressive.

12345678910111213141516
// Using null and undefined in variables let loggedInUser: string | null = null; loggedInUser = "Alice"; let configValue: string | undefined; configValue = "enabled"; // Function returning void function logMessage(message: string): void { console.log("Log:", message); } // Function that never returns (throws an error) function throwError(errorMsg: string): never { throw new Error(errorMsg); }
copy

The null and undefined types represent the absence of a value. You can use null to indicate that a variable is intentionally empty, as shown with the loggedInUser variable above. Here, loggedInUser can be either a string or null, making it clear when a user is not logged in. The undefined type is used when a variable might not be assigned a value at all, such as with configValue. By allowing string | undefined, you signal that configValue may not always be set.

The void type is most commonly used for functions that do not return a value. The logMessage function in the code sample is a perfect example. By specifying a return type of void, you make it clear that the function is only intended to perform a side effect (like logging) and will not produce a return value.

The never type is reserved for functions that never finish normally. This usually means the function always throws an error or never ends, such as with the throwError function. By declaring its return type as never, you make it explicit that this function will not return to the caller under any circumstances.

Choosing the correct special type improves your code's clarity and helps TypeScript catch mistakes early. Use null and undefined to describe missing values, void for functions with no return, and never for functions that never complete.

question mark

Which statement about the void and never types in TypeScript is correct?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. 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

bookSpecial Types

Deslize para mostrar o menu

TypeScript provides several special types that help you describe unusual or edge-case values in your code. Understanding these types lets you write code that is both safer and more expressive.

12345678910111213141516
// Using null and undefined in variables let loggedInUser: string | null = null; loggedInUser = "Alice"; let configValue: string | undefined; configValue = "enabled"; // Function returning void function logMessage(message: string): void { console.log("Log:", message); } // Function that never returns (throws an error) function throwError(errorMsg: string): never { throw new Error(errorMsg); }
copy

The null and undefined types represent the absence of a value. You can use null to indicate that a variable is intentionally empty, as shown with the loggedInUser variable above. Here, loggedInUser can be either a string or null, making it clear when a user is not logged in. The undefined type is used when a variable might not be assigned a value at all, such as with configValue. By allowing string | undefined, you signal that configValue may not always be set.

The void type is most commonly used for functions that do not return a value. The logMessage function in the code sample is a perfect example. By specifying a return type of void, you make it clear that the function is only intended to perform a side effect (like logging) and will not produce a return value.

The never type is reserved for functions that never finish normally. This usually means the function always throws an error or never ends, such as with the throwError function. By declaring its return type as never, you make it explicit that this function will not return to the caller under any circumstances.

Choosing the correct special type improves your code's clarity and helps TypeScript catch mistakes early. Use null and undefined to describe missing values, void for functions with no return, and never for functions that never complete.

question mark

Which statement about the void and never types in TypeScript is correct?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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