TypeScript cheatsheet
Syntax snippets and quick notes for revision.
TypeScript Introduction
interface Order {
id: number;
total: number;
status: "paid" | "pending";
customer: string;
}
const order: Order = { id: 42, total: 50, status: "paid", customer: "Asha" };
console.log(order);Use this typescript introduction pattern when a TypeScript task needs a small, readable starting point.
TypeScript Setup
interface Order {
id: number;
total: number;
status: "paid" | "pending";
customer: string;
}
const order: Order = { id: 42, total: 51, status: "paid", customer: "Asha" };
console.log(order);Use this typescript setup pattern when a TypeScript task needs a small, readable starting point.
Basic Types
interface Order {
id: number;
total: number;
status: "paid" | "pending";
customer: string;
}
const order: Order = { id: 42, total: 52, status: "paid", customer: "Asha" };
console.log(order);Use this basic types pattern when a TypeScript task needs a small, readable starting point.
Arrays and Tuples
const lesson: string = "Arrays and Tuples";
const duration: number = 53;
console.log(`Learning ${lesson} in ${duration} minutes.`);Use this arrays and tuples pattern when a TypeScript task needs a small, readable starting point.
Enums
const lesson: string = "Enums";
const duration: number = 54;
console.log(`Learning ${lesson} in ${duration} minutes.`);Use this enums pattern when a TypeScript task needs a small, readable starting point.
Interfaces
interface Order {
id: number;
total: number;
status: "paid" | "pending";
customer: string;
}
const order: Order = { id: 42, total: 55, status: "paid", customer: "Asha" };
console.log(order);Use this interfaces pattern when a TypeScript task needs a small, readable starting point.
Type Aliases
interface Order {
id: number;
total: number;
status: "paid" | "pending";
customer: string;
}
const order: Order = { id: 42, total: 56, status: "paid", customer: "Asha" };
console.log(order);Use this type aliases pattern when a TypeScript task needs a small, readable starting point.
Union and Intersection Types
interface Order {
id: number;
total: number;
status: "paid" | "pending";
customer: string;
}
const order: Order = { id: 42, total: 57, status: "paid", customer: "Asha" };
console.log(order);Use this union and intersection types pattern when a TypeScript task needs a small, readable starting point.
Functions in TS
const lesson: string = "Functions in TS";
const duration: number = 58;
console.log(`Learning ${lesson} in ${duration} minutes.`);Use this functions in ts pattern when a TypeScript task needs a small, readable starting point.
Classes in TS
const lesson: string = "Classes in TS";
const duration: number = 59;
console.log(`Learning ${lesson} in ${duration} minutes.`);Use this classes in ts pattern when a TypeScript task needs a small, readable starting point.
Generics Basics
function wrapValue<T>(value: T): { data: T } {
return { data: value };
}
const orderWrap = wrapValue<number>(60);
console.log(orderWrap);Use this generics basics pattern when a TypeScript task needs a small, readable starting point.
Generics Advanced
function wrapValue<T>(value: T): { data: T } {
return { data: value };
}
const orderWrap = wrapValue<number>(61);
console.log(orderWrap);Use this generics advanced pattern when a TypeScript task needs a small, readable starting point.