What are Null, Undefined, and Unknown Types in TypeScript?
Exploring the usefulness of TypeScript's built-in types, this article focuses on Null, Undefined, and Unknown types and their applications in defining variables and properties.
Introduction
TypeScript is a popular programming language that extends JavaScript by adding optional static typing, allowing for better type checking and catching errors at compile time. In TypeScript, there are several built-in types that developers can use to define the types of their variables and properties. In this article, we'll explore three of these types: Null, Undefined, and Unknown.
Null Type
Description: Null is a built-in type in TypeScript that represents the intentional absence of any value. It's a way of saying that a variable exists, but its value is nothing. You would use null when you want to explicitly represent the absence of a value.
Analogy: Imagine an e-commerce website that has a Shopping Cart feature - customers have the option of adding items that they want to their cart to purchase later. If a customer adds nothing to their cart and tries to access its contents, we can say that this value is null - because the cart itself exists, but it doesn't have any items in it.
Undefined Type
Description: Similar to Null, Undefined is a built-in type that represents the lack of value for a variable because the variable has not been assigned a value. The variable has not been initialized. Matter of fact, it's automatically assigned to uninitialized variables. So you can say that this is perhaps the unintentional lack of value.
Analogy: Going back to our e-commerce example, imagine that this website hasn't had the time to implement a Shopping Cart feature. If we were to somehow try and access the cart's contents, we would say that this value is undefined - because the cart itself does not exist, and no items could exist in it, either!
Unknown Type
Description: Unknown is a relatively new addition to TypeScript (introduced in TypeScript 3.0) and is a type that represents values that are not known or expected. It's a way of representing values when their type is not known or when you want to be explicit that a value could be of any type.
Example: For example, imagine that we receive data from an external API, and we're not sure what the data type is. In this case, we can use the unknown type to declare the variable's type, and then we can perform runtime checks to determine the actual data type.
Analogy: Back to our store, imagine that a customer has added a pair of shoes to their Shopping Cart with a selected size and color, but hasn't decided if they want custom aglets. The customer may choose from the available options, they may design their own, or they may not want aglets at all. Until they make their decision, this property of the product is simply Unknown - it may be anything. Note that it can't be null by default - because that implies that the customer does not want aglets. That may be the case, but imagine the customer just forgets to decide and they get shoes in the mail with frayed laces.
Conclusion
In conclusion, TypeScript provides developers with several built-in types to help define the types of their variables and properties. Null, Undefined, and Unknown are three such types, with Null representing intentional absence of value, Undefined representing lack of value due to uninitialized variables, and Unknown representing values that are not known or expected. Understanding these types and their use cases can help developers write more reliable and robust code.