0% found this document useful (0 votes)
15 views5 pages

Typescript Backend Cheatsheet Final

This TypeScript cheat sheet focuses on backend development, covering basic types, arrays, tuples, objects, interfaces, functions, and more. It includes examples of union and intersection types, enums, generics, utility types, and express request/response typing. Additionally, it provides recommended development dependencies for backend projects and tips for using TypeScript in VSCode.

Uploaded by

contentcringe14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Typescript Backend Cheatsheet Final

This TypeScript cheat sheet focuses on backend development, covering basic types, arrays, tuples, objects, interfaces, functions, and more. It includes examples of union and intersection types, enums, generics, utility types, and express request/response typing. Additionally, it provides recommended development dependencies for backend projects and tips for using TypeScript in VSCode.

Uploaded by

contentcringe14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TypeScript Cheat Sheet (Backend Focused with Comments)

Basic Types

// Basic string, number, boolean types

let username: string = "Alex";

let age: number = 25;

let isAdmin: boolean = true;

// Special types: any (unsafe) and unknown (safe)

let anything: any = "Could be anything";

let unknownValue: unknown = 123; // Must be type-checked before use

Arrays & Tuples

// Array of strings

let users: string[] = ["Alice", "Bob"];

// Alternative syntax for array of numbers

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

// Tuple: fixed types and length

let userTuple: [string, number] = ["Alice", 30];

Objects & Interfaces

// Define the structure of an object

interface User {

id: string;

name: string;

email: string;

// Create an object that matches the interface

const user: User = {


id: "abc123",

name: "John Doe",

email: "[email protected]"

};

Functions

// Function with typed parameter and return value

function greet(name: string): string {

return `Hello, ${name}`;

// Function that returns nothing (void)

function logMessage(message: string): void {

console.log(message);

Union & Intersection Types

// Union type: can be either string or number

type ID = string | number;

// Intersection type: combines multiple types

interface Address {

city: string;

country: string;

type UserWithAddress = User & Address;

Enums

// Enum for predefined constants

enum Role {

ADMIN = "admin",

USER = "user",
GUEST = "guest"

Literal Types

// Allow only specific string values

type Method = "GET" | "POST" | "PUT" | "DELETE";

Generics

// Function that works with any type

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

return arg;

// Using the generic function

const result = identity<number>(42);

// Generic interface for API response

interface ApiResponse<T> {

status: "success" | "error";

data: T;

Utility Types

// Partial: make all fields optional

Partial<User>

// Pick: select specific fields from an interface

Pick<User, "id" | "email">

// Omit: remove specific fields from an interface

Omit<User, "email">

Express Request / Response Typing


import { Request, Response } from "express";

// Strongly typed request and response objects

app.post('/login', (req: Request, res: Response) => {

const { email, password } = req.body;

res.send('Logged in');

});

Special Types

// Function that never returns (throws an error)

function throwError(message: string): never {

throw new Error(message);

Common Backend Type Aliases

// Custom type names for clarity

type UserId = string;

type OrderStatus = "pending" | "completed" | "cancelled";

// Interface for login request body

interface LoginBody {

email: string;

password: string;

// Generic API response type

type CreateUserResponse = ApiResponse<User>;

Bonus: Recommended Dev Dependencies for Backend Project

npm install express

npm install -D typescript ts-node @types/node @types/express

typescript -> Type checker and compiler


ts-node -> Run .ts files without compiling manually

@types/node -> Type definitions for Node.js core modules

@types/express -> Type definitions for Express

Quick Tip

Hover over variables in VSCode - TypeScript will show you the type immediately for faster learning!

You might also like