TypeScript is a strongly typed superset of JavaScript that provides optional static typing and other features to make writing and maintaining large-scale JavaScript applications easier. TypeScript is maintained by Microsoft and is used in many popular projects such as Angular, React, and Vue.js.
One of the main benefits of using TypeScript is that it provides additional type safety compared to JavaScript. With TypeScript, you can specify the types of variables, functions, and other objects, which helps catch errors at compile-time and provides better tooling and code completion in IDEs. Additionally, TypeScript has features such as interfaces and classes that allow for better code organization and maintainability.
For developers new to TypeScript or those looking for a quick reference, the cheat sheet above provides an overview of some of the most commonly used language features, syntax, and functions. The cheat sheet is divided into several sections, each with a table of related concepts and examples. The sections cover everything from basic types and syntax to more advanced features such as classes, interfaces, and generics.
Cheat Sheet
Table of Contents
Basic Types
Functions
Interfaces
Classes
Enums
Type Inference
Type Aliases
Union Types
Intersection Types
Type Guards
Generics
Type Assertions
Modules
Decorators
1. Basic Types
Type
Description
Example
number
Represents numeric values
let count: number = 5;
string
Represents textual values
let message: string = “Hello, world!”;
boolean
Represents logical values
let isDone: boolean = false;
null
Represents the absence of any value
let data: null = null;
undefined
Represents a value that has not been initialized
let data: undefined = undefined;
void
Represents the absence of a value
function logMessage(message: string): void { console.log(message); }
any
Represents any value
let data: any = “hello”;
never
Represents a value that will never occur
function raiseError(): never { throw new Error(“Something went wrong!”); }
2. Functions
Syntax
Description
Example
Function Declaration
Defines a named function
function add(x: number, y: number): number { return x + y; }
Function Expression
Defines a function expression that can be assigned to a variable
const add = function(x: number, y: number): number { return x + y; };
Arrow Function Expression
Defines a function expression using the arrow syntax
const add = (x: number, y: number): number => { return x + y; };
3. Interfaces
Syntax
Description
Example
Interface Declaration
Defines a new interface
interface Person { name: string; age: number; }
Optional Properties
Defines optional properties
interface Person { name: string; age?: number; }
Readonly Properties
Defines read-only properties
interface Point { readonly x: number; readonly y: number; }
Index Signatures
Defines an index signature to allow any property name
interface Dictionary { [key: string]: any; }
4. Classes
Syntax
Description
Example
Class Declaration
Defines a new class
class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } }
Defines public, private, and protected access modifiers for class members
class Person { public name: string; private age: number; protected phone: string; constructor(name: string, age: number, phone: string) { this.name = name; this.age = age; this.phone = phone; } }
Abstract Classes
Defines abstract classes that cannot be instantiated
abstract class Animal { abstract makeSound(): void; }
5. Enums
Syntax
Description
Example
Enum Declaration
Defines a new enum
enum Color { Red, Green, Blue }
Enum Values
Specifies numeric values for enum members
enum Color { Red = 1, Green = 2, Blue = 3 }
String Enums
Specifies string values for enum members
enum Color { Red = “RED”, Green = “GREEN”, Blue = “BLUE” }
Computed Enums
Specifies computed values for enum members
enum Color { Red = 1, Green = 2, Blue = 3, Yellow = Red + Green }
6. Type Inference
Syntax
Description
Example
Basic Type Inference
Infers types based on assigned values
let count = 5; // count is inferred as number
Best Common Type
Infers the best common type of a set of values
let values = [1, 2, true]; // values is inferred as (number | boolean)[]
Contextual Typing
Infers types based on their context
let handler = (event: MouseEvent) => { console.log(event.clientX); };
7. Type Aliases
Syntax
Description
Example
Type Alias Declaration
Defines a new type alias
type Point = { x: number; y: number; };
Union Type Aliases
Defines a union of types as a new type alias
type ID = string | number;
Intersection Type Aliases
Defines an intersection of types as a new type alias
type Employee = Person & { department: string; };
8. Union Types
Syntax
Description
Example
Union Types
Defines a type that can be one of several types
let value: string | number;
Type Guards
Uses typeof, instanceof, or custom checks to narrow union types
function isNumber(value: string | number): value is number { return typeof value === “number”; } if (isNumber(value)) { // value is narrowed to number }
9. Intersection Types
Syntax
Description
Example
Intersection Types
Defines a type that is a combination of several types
type Employee = Person & { department: string; }; let employee: Employee = { name: “John”, age: 30, department: “IT” };
10. Type Guards
Syntax
Description
Example
typeof Type Guards
Uses typeof to narrow types
let value: string | number; if (typeof value === “string”) { // value is narrowed to string }
instanceof Type Guards
Uses instanceof to narrow types
function isAnimal(value: any): value is Animal { return value instanceof Animal; } if (isAnimal(value)) { // value is narrowed to Animal }
Custom Type Guards
Defines custom functions to narrow types
function isNumber(value: string | number): value is number { return typeof value === “number”; } if (isNumber(value)) { // value is narrowed to number }
11. Generics
Syntax
Description
Example
Generic Type Parameters
Defines a generic type parameter
function identity<T>(value: T): T { return value; }
Generic Constraints
Defines constraints on generic type parameters
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; }
function identity<T = any>(value: T): T { return value; }
Generic Utility Types
Provides built-in generic utility types
type Partial<T> = { [P in keyof T]?: T[P]; }; type Readonly<T> = { readonly [P in keyof T]: T[P]; }; type Record<K extends keyof any, T> = { [P in K]: T };
12. Type Assertions
Syntax
Description
Example
Type Assertions
Asserts a type on a value
let value: any = “hello”; let length: number = (value as string).length;
Non-null Assertion Operator
Asserts a value is not null or undefined
let value: string | null = “hello”; let length: number = value!.length;
13. Modules
Syntax
Description
Example
Import Declarations
Imports code from other modules
import { MyComponent } from “./my-component”;
Export Declarations
Exports code from a module
export function myFunction() { /* … */ }
Default Exports
Exports a default value from a module
export default class MyComponent { /* … */ }
Namespace Imports
Imports all exported values from a module into a namespace