0% found this document useful (0 votes)
22 views

TypeScript Cheat Sheet

This TypeScript Cheat Sheet provides a comprehensive overview of TypeScript fundamentals, including installation, types, functions, interfaces, classes, generics, advanced types, modules, and utility types. It includes code snippets for each topic to illustrate usage. The document serves as a quick reference for developers working with TypeScript.

Uploaded by

mrbanala.m51
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

TypeScript Cheat Sheet

This TypeScript Cheat Sheet provides a comprehensive overview of TypeScript fundamentals, including installation, types, functions, interfaces, classes, generics, advanced types, modules, and utility types. It includes code snippets for each topic to illustrate usage. The document serves as a quick reference for developers working with TypeScript.

Uploaded by

mrbanala.m51
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

TypeScript Cheat Sheet

1. Basics

• Install TypeScript:

bash

Copy

npm install -g typescript


• Compile TypeScript:
bash

Copy

tsc file.ts
• Hello World:
typescript

Copy

console.log("Hello, World!");

2. Types

• Primitive Types:

typescript

Copy

let num: number = 10;


let str: string = "Hello";
let bool: boolean = true;
• Arrays:
typescript

Copy

let numbers: number[] = [1, 2, 3];


let strings: Array<string> = ["a", "b", "c"];
• Tuples:
typescript

Copy
let tuple: [string, number] = ["Alice", 25];
• Enums:
typescript

Copy

enum Color { Red, Green, Blue }


let color: Color = Color.Green;
• Any:
typescript

Copy

let anything: any = "Can be anything";


• Void:
typescript

Copy

function logMessage(): void {


console.log("This is a message");
}
• Null and Undefined:
typescript

Copy

let u: undefined = undefined;


let n: null = null;

3. Functions

• Function with Types:

typescript

Copy

function add(a: number, b: number): number {


return a + b;
}
• Optional Parameters:
typescript

Copy

function greet(name: string, age?: number): string {


return age ? `${name} is ${age} years old` : `Hello, ${name}`;
}
• Default Parameters:
typescript

Copy

function greet(name: string, age: number = 25): string {


return `${name} is ${age} years old`;
}
• Arrow Functions:
typescript

Copy

const add = (a: number, b: number): number => a + b;

4. Interfaces

• Define Interface:

typescript

Copy

interface Person {
name: string;
age: number;
}
• Use Interface:
typescript

Copy

let person: Person = { name: "Alice", age: 25 };


• Optional Properties:
typescript

Copy

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

5. Classes
• Define Class:

typescript

Copy

class Animal {
name: string;

constructor(name: string) {
this.name = name;
}

speak(): void {
console.log(`${this.name} makes a noise`);
}
}
• Inheritance:
typescript

Copy

class Dog extends Animal {


breed: string;

constructor(name: string, breed: string) {


super(name);
this.breed = breed;
}

speak(): void {
console.log(`${this.name} barks`);
}
}

6. Generics

• Generic Function:

typescript

Copy

function identity<T>(arg: T): T {


return arg;
}
• Generic Class:
typescript

Copy
class Box<T> {
value: T;

constructor(value: T) {
this.value = value;
}
}

7. Advanced Types

• Union Types:

typescript

Copy

let value: string | number;


value = "Hello";
value = 10;
• Type Aliases:
typescript

Copy

type StringOrNumber = string | number;


let value: StringOrNumber;
• Intersection Types:
typescript

Copy

type Name = { name: string };


type Age = { age: number };
type Person = Name & Age;

8. Modules

• Export:

typescript

Copy

export class MyClass { }


export function myFunction() { }
• Import:
typescript
Copy

import { MyClass, myFunction } from './myModule';

9. Utility Types

• Partial:

typescript

Copy

interface Person {
name: string;
age: number;
}
let partialPerson: Partial<Person> = { name: "Alice" };
• Readonly:
typescript

Copy

let readonlyPerson: Readonly<Person> = { name: "Alice", age: 25 };


• Record:
typescript

Copy

let personRecord: Record<string, number> = { "Alice": 25, "Bob": 30 };

You might also like