# Typescript Fundamentals Cheatsheet

Table of Contents

TypeScript is a strongly-typed superset of JavaScript that compiles to plain JavaScript. This cheatsheet covers the most important syntax and features you need to get started.

Basic Types

let isDone: boolean = false
let age: number = 42
let firstName: string = 'Alice'
let list: number[] = [1, 2, 3]
let tuple: [string, number] = ['hello', 10]

Any and Unknown

let notSure: any = 4
notSure = 'maybe a string'
let value: unknown = 'Hello'
if (typeof value === 'string') {
console.log(value.toUpperCase())
}

Functions

function add(x: number, y: number): number {
return x + y
}
const greet = (name: string): void => {
console.log(`Hello, ${name}`)
}

Interfaces

interface User {
name: string
age?: number // optional
}
const user: User = { name: 'Bob' }

Type Aliases

type ID = string | number
let userId: ID = 123

Enums

enum Direction {
Up,
Down,
Left,
Right,
}
let dir: Direction = Direction.Up

Generics

function identity<T>(arg: T): T {
return arg
}
let output = identity<string>('Hello')

Classes

class Person {
constructor(
public name: string,
private age: number,
) {}
greet() {
console.log(`Hi, I'm ${this.name}`)
}
}
const alice = new Person('Alice', 30)
alice.greet()

Modules

math.ts
export function add(a: number, b: number): number {
return a + b
}
// file: app.ts
import { add } from './math'
console.log(add(2, 3))

Type Assertions

let someValue: unknown = 'this is a string'
let strLength: number = (someValue as string).length

Utility Types

  • Partial<T> — all properties optional
  • Readonly<T> — all properties read-only
  • Pick<T, K> — select specific properties
  • Omit<T, K> — remove specific properties

Example:

interface Todo {
title: string
description: string
}
type TodoPreview = Pick<Todo, 'title'>

Compilation

Terminal window
tsc file.ts

Compile and watch for changes:

Terminal window
tsc -w

Final Thoughts

This cheatsheet gives you the essentials for working productively with TypeScript. Master these fundamentals before moving on to advanced typing and complex generics.

My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


More Posts