TypeScript Programming Language Cheatsheet

TypeScript has gained immense popularity in the world of web development due to its ability to add static typing to JavaScript. Developed and maintained by Microsoft, TypeScript extends JavaScript by introducing optional static types, which can catch common errors and improve code maintainability. If you’re new to TypeScript or looking for a quick reference guide, this cheatsheet is designed to help you get started and navigate the language efficiently.

Basics

1. Variable Declaration

let variableName: type = value;

2. Type Annotations

let variableName: number = 10;
let stringVariable: string = "Hello, TypeScript!";
let booleanVariable: boolean = true;

3. Functions

function add(a: number, b: number): number {
    return a + b;
}

4. Arrays

let numbers: number[] = [1, 2, 3];
let strings: Array<string> = ["one", "two", "three"];

5. Interfaces

interface Person {
    name: string;
    age: number;
}

let user: Person = {
    name: "John Doe",
    age: 30
};

Advanced Types

1. Union Types

let result: number | string;
result = 10;
result = "Hello";

2. Intersection Types

type Car = {
    brand: string;
};

type ElectricCar = {
    batteryLife: number;
};

let hybridCar: Car & ElectricCar = {
    brand: "Tesla",
    batteryLife: 300
};

3. Type Aliases

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

let coordinate: Point = { x: 10, y: 20 };

4. Enums

enum Color {
    Red,
    Green,
    Blue
}

let selectedColor: Color = Color.Green;

Advanced Concepts

1. Generics

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

let output = identity<string>("Hello, Generics!");

2. Decorators

function log(target: any, key: string) {
    console.log(`${key} was called`);
}

class Example {
    @log
    someMethod() {
        // Method logic here
    }
}

3. Modules

// math.ts
export function add(a: number, b: number): number {
    return a + b;
}

// main.ts
import { add } from "./math";
let result = add(10, 20);

Type Assertion

let someValue: any = "Hello, TypeScript!";
let strLength: number = (someValue as string).length;

Conclusion

This TypeScript cheatsheet provides a quick reference for essential syntax and concepts. Whether you’re a beginner or an experienced developer, having this cheatsheet handy can help you write TypeScript code more effectively. Keep exploring the TypeScript documentation for more in-depth information and examples as you continue to enhance your web development skills. Happy coding!

For more details, always refer to the official TypeScript documentation.

1. What is TypeScript, and how does it differ from JavaScript?

TypeScript is a superset of JavaScript that adds optional static typing to the language. It enables developers to catch common errors during development and improves code maintainability. Unlike JavaScript, TypeScript introduces static types, interfaces, and other features that facilitate better tooling and code organization.

2. How do I migrate an existing JavaScript project to TypeScript?

Migrating a JavaScript project to TypeScript can be done incrementally. Start by renaming your existing .js files to .ts and fixing any type-related errors that TypeScript points out. You can gradually add type annotations, interfaces, and other TypeScript features to enhance your codebase. Additionally, leverage the TypeScript compiler’s configuration to tailor the migration process to your project’s needs.

3. What are the key benefits of using TypeScript?

TypeScript offers several benefits, including:
Static Typing: Helps catch errors during development.
Improved Tooling: Enables better code navigation and refactoring support.
Enhanced Readability: Type annotations serve as documentation, making code more understandable.
Code Maintainability: Reduces the likelihood of runtime errors and facilitates collaboration in larger projects.
Compatibility: TypeScript code can be transpiled to JavaScript, making it compatible with existing JavaScript libraries and frameworks.

4. Can TypeScript be used for both frontend and backend development?

Yes, TypeScript can be used for both frontend and backend development. On the frontend, it is commonly used with popular frameworks like Angular, React, or Vue.js. On the backend, TypeScript can be used with Node.js. Its ability to be transpiled to JavaScript allows developers to use TypeScript across the full stack, promoting code reuse and consistency.

5. How does TypeScript handle asynchronous programming?

TypeScript supports asynchronous programming through features like async/await. When using these constructs, TypeScript enables developers to define asynchronous functions and provides type checking for Promises. This helps catch potential errors related to asynchronous code during development, making it safer and more reliable.