TypeScript Types Cheat Sheet

TypeScript Types Cheat Sheet

TypeScript's type system empowers developers to write more robust and maintainable code by enforcing static types. This cheat sheet covers a wide range of TypeScript types, featuring detailed code examples and brief descriptions for each topic, serving as an invaluable reference for developers.


  1. Basic Types:

Basic types form the foundation of TypeScript's type system, providing representations for fundamental data types such as numbers, strings, booleans, arrays, tuples, and any.

let age: number = 25;
let name: string = "John";
let isValid: boolean = true;
let numbers: number[] = [1, 2, 3, 4, 5];
let person: [string, number] = ["John", 25];
let data: any = "Some data";        

Basic types provide static type checking and help catch errors during development.

2. Union Types:

Union types allow variables to accept values of multiple types, providing flexibility in data handling and improving code expressiveness.

let result: number | string;
result = 10;        // Valid
result = "success"; // Valid        

Union types enable developers to handle scenarios where a variable can have more than one possible type.

3. Intersection Types:

Intersection types combine multiple types into one, enabling developers to create new types by merging existing ones, enhancing code flexibility and expressiveness.

type Printable = {
    print(): void;
};

type Loggable = {
    log(): void;
};

type Logger = Printable & Loggable;        

Intersection types allow objects to have properties from multiple types, providing a way to compose complex types.

4. Type Guards:

Type guards are expressions that help narrow down the type of a variable within a conditional block, improving type inference and enabling safer code execution.

function isNumber(value: any): value is number {
    return typeof value === 'number';
}

if (isNumber(input)) {
    // Input is a number
}        

Type guards are essential for handling dynamic typing scenarios and ensuring type safety.

5. Generics:

Generics enable the creation of reusable components with flexible types, enhancing code abstraction and maintainability by allowing types to be parameterized.

function identity<T>(arg: T): T {
    return arg;
}

let result = identity<number>(5); // result is of type number        

Generics provide a way to write functions and classes that work with any data type.

  1. Type Aliases:

Type aliases provide a convenient way to create custom names for types, improving code readability and maintainability by encapsulating complex type definitions.

type Point = {
    x: number;
    y: number;
};

type UserID = string | number;        

Type aliases simplify complex type definitions and provide meaningful names for types used throughout the codebase

Conclusion

Mastering TypeScript types is essential for writing clean, maintainable, and error-free code. By understanding the concepts covered in this cheat sheet and using the provided code examples, developers can leverage TypeScript's type system to enhance the reliability, readability, and scalability of their projects. Keep this cheat sheet handy as a quick reference when working with types in TypeScript. Happy coding!

要查看或添加评论,请登录

Adrian Birta的更多文章

社区洞察

其他会员也浏览了