Special Datatypes in TypeScript
In this section, we focus more on the datatypes that are specific to TypeScript, which are the gateway for you to start properly delving into some TypeScript Coding.
In the first module, we told you that TypeScript is a βflavorβ or βvariantβ of JavaScript. These datatypes add up onto the datatypes we learnt in the previous module.
You will explore various data types such as **tuple, enum, any, void and learn how to use them effectively in your TypeScript programs.
1. Tupleβ
The 'Tuple' datatype is used to represent an array with a fixed number of elements, each with a specific type.
Example: let tuple: [number, string] = [1, "Alice"];
2. Enumβ
The 'Enum' datatype is used to define a set of named constants.
Example:
typescript
enum Color { Red, Green, Blue }
let c: Color = Color.Green;
3. Anyβ
The 'Any' datatype is used to represent any type, and is used to opt out of type-checking.
Example: let variable: any = "Could be anything";
4. Voidβ
The 'Void' datatype is used to represent the absence of any type, commonly used as the return type of functions that do not return a value.
Example: function log(message: string): void { console.log(message); }
5. Neverβ
The 'Never' datatype is used to represent the type of values that never occur, typically used for functions that always throw an error or never return.
Example: function error(message: string): never { throw new Error(message); }
6. Unknownβ
The 'Unknown' datatype is used to represent a type-safe counterpart to any.
Example: let uncertain: unknown = 4;
7. Intersection Typesβ
The 'Intersection Types' datatype is used to combine multiple types into one.
Example: type Combined = { a: number } & { b: string };
8. Union Typesβ
The 'Union Types' datatype is used to represent a value that can be one of several types.
Example: let value: number | string = "hello";
9. Literal Typesβ
The 'Literal Types' datatype is used to represent specific values.
Example: let direction: "up" | "down" = "up";
10. Type Aliasesβ
The 'Type Aliases' datatype is used to provide a name for any type.
Example: type StringOrNumber = string | number;
11. Interfacesβ
The 'Interfaces' datatype is used to describe the shape of an object.
Example:
typescript
interface Person {
name: string;
age: number;
}
let user: Person = { name: "Alice", age: 25 };