Special 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); }
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.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 8.33
Special Types
Glissez pour afficher le 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); }
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.
Merci pour vos commentaires !