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:
TypeScript ThisParameterType<Type> Utility Type
Next article icon

TypeScript InstanceType<Type> Utility Type

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to learn about InstanceType<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the InstanceType<Type> utility type is used to extract the instance type of a constructor function or class type. It allows you to infer the type of instances that can be created from a constructor or class.

Syntax

type T1 = InstanceType<Type>;

Parameters

  • Type: This is the type parameter that represents the constructor function or class type from which you want to extract the instance type.
  • T1: This is the name of the utility type that extracts the instance type.

Example 1

In this example,

  • We have a Person class with a constructor that takes a name and an age.
  • We use InstanceType<typeof Person> to extract the instance type of the Person class, which is equivalent to { name: string; age: number; }.
  • We create an instance of Person using new Person("Alice", 30) and assign it to the person variable.
  • TypeScript infers that person is of type PersonInstance, which allows us to access the name and age properties of the person object with type safety.
JavaScript
class Person {     constructor(public name: string, public age: number) { } }  type PersonInstance = InstanceType<typeof Person>;  const person: PersonInstance = new Person("Alice", 30);  console.log(person.name); // "Alice" console.log(person.age); // 30 

Output

z22

Example 2

In this example,

  • We have a Product class with a constructor that takes a name and a price.
  • We also have a ShoppingCart class that can hold instances of Product.
  • InstanceType<typeof Product> is used to specify the type of products that can be added to the shopping cart.
  • We create instances of Product (e.g., laptop and smartphone) and an instance of ShoppingCart (e.g., cart).
  • We add products to the cart using the addItem method.
  • We calculate the total price of the items in the cart using the getTotalPrice method
JavaScript
class Product {     constructor(public name: string, public price: number) { } }  class ShoppingCart {     private items: InstanceType<typeof Product>[] = [];      addItem(product: InstanceType<typeof Product>): void {         this.items.push(product);     }      getTotalPrice(): number {         return this.items.reduce((total, product                 ) => total + product.price, 0);     } }  // Create instances of Product const laptop = new Product("Laptop", 1000); const smartphone = new Product("Smartphone", 500);  // Create an instance of ShoppingCart const cart = new ShoppingCart();  // Add products to the cart cart.addItem(laptop); cart.addItem(smartphone);  // Get the total price of the items in the cart const totalPrice = cart.getTotalPrice();  // Output: Total Price: $1500 console.log(`Total Price: $${totalPrice}`); 

Output

z23

Reference: https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype




Next Article
TypeScript ThisParameterType<Type> Utility Type
author
21mcsrltd
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • TypeScript
  • Geeks Premier League 2023

Similar Reads

  • TypeScript ThisType<Type> Utility Type
    The TypeScript ThisType<Type> utility type allows you to define the type of this within a specific object or function context, providing precise type checking for methods and properties accessed through this. The ThisType<Type> utility type is primarily used to provide type annotations f
    3 min read
  • TypeScript ReturnType <Type> Utility Type
    The ReturnType<Type> utility type in TypeScript extracts and infers the return type of a given function type. It enhances type safety and reusability by allowing developers to dynamically determine the type of values returned by functions without manually specifying them. Syntaxtype ResultType
    3 min read
  • TypeScript ThisParameterType<Type> Utility Type
    In this article, we are going to learn about ThisParameterType<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the ThisParameterType<Type> utility type is used to extract the type of this pa
    3 min read
  • Typescript Record<Keys, Type> Utility Type
    In this article, we are going to learn about the Record<Keys, Type> in Typescript. TypeScript is a programming language that is a strict syntactical superset of JavaScript. It adds optional static typing and class-based object-oriented programming to JavaScript, one of the features is Record
    4 min read
  • TypeScript Extract<Type, Union> Utility Type
    In this article, we are going to learn about Extract<Type, Union> utility type in TypeScript, TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tools at any scale. Extract<Type, Union> utility type is used to extract a subset of types from a
    4 min read
  • TypeScript NonNullable<Type> Utility Type
    In this article, we are going to learn about NonNullable<Type> Utility Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, the NonNullable<Type> utility is used to create a new type by removing null and undef
    2 min read
  • TypeScript Creating Types from Utility Type
    TypeScript Creating Types from Utility Type increases the code readability and ease of use when using any complex operations and values, there are different ways to do the type creation with the existing ones. TypeScript Creating Types from Utility Type:Generics: It basically allows us to declare th
    3 min read
  • TypeScript - Recursive Types and Utility Types
    TypeScript adds strong typing to JavaScript. Recursive Types define types that refer to themselves, useful for trees or nested objects. Utility Types simplify type changes, like making properties optional or read-only. These tools help write clear and flexible code. Recursive Types in TypeScriptA re
    4 min read
  • TypeScript Utility Types
    TypeScript utility types are predefined constructs that facilitate common type transformations, enhancing code flexibility and maintainability. They allow developers to modify existing types easily, such as by making properties optional or read-only.Utility types help in creating more expressive and
    5 min read
  • TypeScript Interface vs Type Statements
    In TypeScript, interface and type are used to define the structure of objects and custom types. While interfaces enforce contracts for classes and support multiple inheritance, types offer flexibility for defining complex and reusable type aliases. Each serves distinct use cases. Table of Content In
    3 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