Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript Tutorial
  • TS Exercise
  • TS Interview Questions
  • TS Cheat Sheet
  • TS Array
  • TS String
  • TS Object
  • TS Operators
  • TS Projects
  • TS Union Types
  • TS Function
  • TS Class
  • TS Generic
Open In App
Next Article:
Introduction to TypeScript
Next article icon

TypeScript Cheat Sheet

Last Updated : 04 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript is a strongly typed, object-oriented, compiled programming language developed and maintained by Microsoft. It is a superset of JavaScript, adding static types and other powerful features to improve development efficiency and code quality. TypeScript is widely used in web development, especially in large-scale applications.

TypeScriptCheat-Sheet-
TypeScript Cheat Sheet

TypeScript Features

Here are some of the main features of TypeScript:

  • Static Typing – Enables type checking at compile time.
  • Object-Oriented – Supports classes, interfaces, and inheritance.
  • Robust & Secure – Helps catch errors early with strict type checks.
  • Platform-Independent – Runs on any platform that supports JavaScript.
  • Portable – Works across different browsers and OS environments.
  • Multithreaded (Asynchronous Support) – Uses async/await and Promises.
  • Interoperable – Compatible with JavaScript and its libraries

Here’s a complete TypeScript cheat sheet that covers everything from installation to advanced topics, and examples.

1. Installation

To use TypeScript, you need to install it globally or locally in your project.

Global Installation

npm install -g typescript

Local Installation

npm install typescript --save-dev

Check Version

tsc --version

Compile TypeScript to JavaScript

tsc filename.ts

For more details, you can explore this article: How to Install TypeScript.

2. Basic Variables

TypeScript provides basic types to define variables with specific data types.

JavaScript
let isDone: boolean = false; // Boolean let count: number = 42; // Number let name: string = "TypeScript"; // String let list: number[] = [1, 2, 3]; // Array of numbers let tuple: [string, number] = ["hello", 10]; // Tuple (fixed-type array) let notSure: any = 4; // Any (dynamic type, avoid using) let nothing: void = undefined; // Void (no type, used for functions) let u: undefined = undefined; // Undefined let n: null = null; // Null 

3. Basic Data Types

TypeDescriptionExample
booleanRepresents true/false values.let isDone: boolean = false;
numberRepresents both integers and floating-point numbers.let count: number = 42;
stringRepresents textual data.let name: string = "TypeScript";
number[]Represents an array of numbers.let list: number[] = [1, 2, 3];
[string, number]Represents a tuple (fixed-type array).let tuple: [string, number] = ["hello", 10];
anyRepresents a dynamic type (use sparingly).let notSure: any = 4;
voidRepresents the absence of a type (used for functions that return nothing).let nothing: void = undefined;
undefinedRepresents an uninitialized variable.let u: undefined = undefined;
nullRepresents an intentional absence of an object value.let n: null = null;

4. Advanced Data Types

TypeScript supports advanced types for more complex scenarios.

Union Types

Union Types allows a variable to hold multiple types.

JavaScript
let value: string | number = "hello"; value = 42; 

Intersection Types

Combines multiple types into one.

JavaScript
type A = { a: number }; type B = { b: string }; type C = A & B; // { a: number, b: string } 

Literal Types

Restricts a variable to a set of specific values.

JavaScript
let direction: "left" | "right" | "up" | "down" = "left"; 

Type Aliases

Type aliases in TypeScript allow you to create a new name for an existing type

JavaScript
type StringOrNumber = string | number; let id: StringOrNumber = "123"; 

5. Functions

Functions in TypeScript can have typed parameters and return values.

Typed Function

Typed functions in TypeScript allow you to define the types of parameters a function accepts and the type of value it returns.

JavaScript
function add(x: number, y: number): number {   return x + y; } 

Optional and Default Parameters

Optional parameters (using ?) allow omitting the argument in function calls, resulting in undefined within the function. Default parameters (using = value) provide a fallback if the argument is not provided.

JavaScript
function greet(name: string, greeting: string = "Hello"): string {   return `${greeting}, ${name}`; } 

Arrow Functions

Arrow functions provide a more concise syntax for defining functions compared to traditional function expressions.

JavaScript
// Concise arrow function (implicit return) const add = (a: number, b: number) => a + b;  // Arrow function with explicit return and block body const greet = (name: string) => { return `Hello, ${name}!`; }; 

6. Interfaces

Interfaces in TypeScript define a contract or shape for data. They specify the properties (and sometimes methods) that an object should have.

JavaScript
interface Person {   name: string;   age: number;   greet(): void; }  const person: Person = {   name: "Alice",   age: 30,   greet() {     console.log(`Hello, my name is ${this.name}`);   }, }; 

7. Classes

Classes in TypeScript provide a blueprint for creating objects. They encapsulate data (properties) and behavior (methods) into a single unit.

JavaScript
class Animal {   name: string;    constructor(name: string) {     this.name = name;   }    speak(): void {     console.log(`${this.name} makes a noise.`);   } }  class Dog extends Animal {   breed: string;    constructor(name: string, breed: string) {     super(name);     this.breed = breed;   }    speak(): void {     console.log(`${this.name} barks.`);   } }  const dog = new Dog("Rex", "Labrador"); dog.speak(); 

8. Generics

Generics in TypeScript allow you to write reusable components that can work with a variety of types without sacrificing type safety.

JavaScript
function identity<T>(arg: T): T {   return arg; }  let output = identity<string>("hello");  class Box<T> {   value: T;    constructor(value: T) {     this.value = value;   } }  const box = new Box<number>(42); 

9. Utility Types

Utility types in TypeScript provide a set of pre-defined type transformations that perform common operations on types.

Partial<T>

Partial<T> is a utility type that takes a type T and constructs a new type where all properties of T are optional.

JavaScript
interface User {   name: string;   age: number; } const partialUser: Partial<User> = { name: "GeeksforGeeks" }; 

Readonly<T>

Readonly<T> is a utility type that takes a type T and creates a new type where all properties of T are read-only.

JavaScript
const readonlyUser: Readonly<User> = { name: "Geeks", age: 30 }; 

Record<K, T>

Record<K, T> is a utility type that constructs an object type whose property keys are K and whose property values are T. K can be a string, number, or symbol literal, and T can be any type.

JavaScript
const userMap: Record<string, User> = {   "1": { name: "Geeks", age: 30 }, }; 

Pick<T, K>

Pick<T, K> constructs a new type by picking a set of properties K (which is a union of string literals) from type T

JavaScript
type UserName = Pick<User, "name">; 

10. Type Assertions

Type assertions in TypeScript allow you to override the compiler's type inference and explicitly tell it what type a value is.

JavaScript
let someValue: any = "this is a string"; let strLength: number = (someValue as string).length; 

11. Modules

Modules in TypeScript allow you to organize your code into separate files, improving code maintainability and reusability.

Exporting

JavaScript
export class MyClass { /* ... */ } export const myFunction = () => { /* ... */ }; 

Importing

JavaScript
import { MyClass, myFunction } from "./myModule"; 

12. Advanced Concepts In Typescript

Conditional Types

JavaScript
type NonNullable<T> = T extends null | undefined ? never : T; 

Mapped Types

JavaScript
type Readonly<T> = {   readonly [P in keyof T]: T[P]; }; 

Template Literal Types

JavaScript
type EventName = `on${Capitalize<string>}`; 

Next Article
Introduction to TypeScript

T

tanmxcwi
Improve
Article Tags :
  • Web Technologies
  • TypeScript

Similar Reads

  • TypeScript Operators
    TypeScript operators are symbols or keywords that perform operations on one or more operands. Below are the different TypeScript Operators: Table of Content TypeScript Arithmetic operatorsTypeScript Logical operatorsTypeScript Relational operatorsTypeScript Bitwise operatorsTypeScript Assignment ope
    7 min read
  • Data types in TypeScript
    In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity. Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Pri
    3 min read
  • TypeScript Numbers
    TypeScript Numbers refer to the numerical data type in TypeScript, encompassing integers and floating-point values. The Number class in TypeScript provides methods and properties for manipulating these values, allowing for precise arithmetic operations and formatting, enhancing JavaScript's native n
    4 min read
  • TypeScript Functions Type
    TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage. Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime erro
    6 min read
  • Introduction to TypeScript
    TypeScript is a syntactic superset of JavaScript that adds optional static typing, making it easier to write and maintain large-scale applications. Allows developers to catch errors during development rather than at runtime, improving code reliability.Enhances code readability and maintainability wi
    5 min read
  • Getting Started with TypeScript
    TypeScript is an open-source programming language developed by Microsoft that extends JavaScript by adding optional static typing to the language. It aims to make JavaScript development more scalable and maintainable, especially for large-scale projects. TypeScript code is transpiled into JavaScript
    4 min read
  • Hello World in TypeScript
    TypeScript is an open-source programming language. It is developed and maintained by Microsoft. TypeScript follows javascript syntactically but adds more features to it. It is a superset of javascript. The diagram below depicts the relationship: Typescript is purely object-oriented with features lik
    3 min read
  • Git Cheat Sheet
    Git Cheat Sheet is a comprehensive quick guide for learning Git concepts, from very basic to advanced levels. By this Git Cheat Sheet, our aim is to provide a handy reference tool for both beginners and experienced developers/DevOps engineers. This Git Cheat Sheet not only makes it easier for newcom
    10 min read
  • TypeScript Other Types to Know About
    TypeScript Other Types to Know About describes that there are many other types that can be used in various ways. like void, object, unknown, never, and function, and how they can be used to enhance the functionality of your code. Other types to know about:void: In TypeScript, a void function is a fu
    2 min read
  • HTML Cheat Sheet
    HTML (HyperText Markup Language) serves as the foundational framework for web pages, structuring content like text, images, and videos. HTML forms the backbone of every web page, defining its structure, content, and interactions. Its enduring relevance lies in its universal adoption across web devel
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences