TypeScript’s Special Types

TypeScript is a powerful tool for ensuring that your code is clean, organized, and error-free. One of the ways it does this is by having several special types that you can use to be more specific about the data you’re working with.

Let’s start with the any type. This is like the wild card of TypeScript. It can be any value and is perfect for when you’re not sure what type of data a variable is going to hold. Think of it like a box that can hold anything.

let x: any = "hello";
x = 123; // valid
x = true; // valid

Next up is the unknown type. This one is similar to any, but it’s a little more restrictive. When you use unknown, you’re saying “I know there’s something in here, but I have no idea what it is.” This type can only be assigned to variables of type unknown or any, and you have to check the type before using it. It’s like opening a present on Christmas morning, you know there’s something inside, but you have to unwrap it to find out what it is.

let x: unknown = "hello";
if (typeof x === "string") {
    x.toUpperCase(); // valid
}

We also have the void type, which represents the absence of a value. This one is perfect for functions that don’t return anything. Think of it like a magician’s trick where the rabbit disappears into thin air.

function display(): void {
    console.log("Hello World!");
}

Then we have null and undefined, which both represent the absence of a value. undefined is the default value for variables that aren’t explicitly initialized, kind of like a blank canvas waiting for an artist to paint on it. null is a little different, it represents a deliberate absence of a value, like a magician making a bird disappear on purpose.

let x: null = null;
let y: undefined = undefined;

The never type is a bit of an oddball. It represents a value that will never occur. It’s perfect for functions that never return a value or for values that should never be used. It’s like trying to find a unicorn in the wild.

function infiniteLoop(): never {
    while (true) {
        // infinite loop
    }
}

We also have Tuple , this type represents a fixed-length array, where the type of a fixed number of elements is known. It’s like a shopping list with a fixed number of items, you know exactly what you need to buy.

let x: [string, number, boolean] = ["hello", 123, true];

Enum is another interesting one, it creates a new enumerated type with a set of named constants. Think of it like a menu at a restaurant, you have a list of options to choose from.

enum Color {Red, Green, Blue}
let backgroundColor: Color = Color.Blue;

Lastly, we have Union and Intersection types. These types represent a value that can be one of several types or all of several types respectively. It’s like having a shape-shifter as a variable, it can be a square or a triangle or both at the same time.

let x: string | number = "hello";
x = 123; // valid
type A = {a: number};
type B = {b: string};
type AB = A & B;
let x: AB = {a: 1, b: "hello"};

And that’s it! Happy coding!