Bird
Raised Fist0
Typescriptprogramming~10 mins

InstanceType type in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - InstanceType type
Define a class
↓
Use InstanceType<Type>
↓
Get the instance type of the class
↓
Use instance type for variables or functions
InstanceType takes a class type and gives you the type of its instance, letting you use that instance type elsewhere.
Execution Sample
Typescript
class Person {
  name: string = '';
}
type PersonInstance = InstanceType<typeof Person>;
const p: PersonInstance = new Person();
This code defines a class Person, then uses InstanceType to get the type of its instance, and creates a variable p of that instance type.
Execution Table
StepActionEvaluationResult
1Define class PersonPerson is a class with property name:stringPerson type created
2Use InstanceType<typeof Person>Extract instance type of PersonPersonInstance is type { name: string }
3Declare variable p: PersonInstancep is typed as the instance type of Personp can access property name
4Assign new Person() to pCreate new instance of Personp is a Person instance with name initialized to ''
5Access p.namep.name is stringAllowed and type safe
6EndNo errorsExecution stops
💡 All steps complete, instance type used correctly
Variable Tracker
VariableStartAfter Step 4Final
Personclass definitionclass definitionclass definition
PersonInstanceundefined{ name: string }{ name: string }
pundefinedPerson instance with name=''Person instance with name=''
Key Moments - 2 Insights
Why do we use typeof Person inside InstanceType?
InstanceType expects a type representing a class constructor, so we use typeof Person to get the class type, not the instance type. See execution_table step 2.
Can we assign an object literal to p without using new Person()?
Yes, because TypeScript uses structural typing and PersonInstance matches { name: string }, so { name: '' } type-checks. But new Person() creates a proper class instance. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table step 2, what does InstanceType<typeof Person> produce?
AThe type of the Person class itself
BThe type of an instance of Person
CA string literal 'Person'
DAn error
💡 Hint
Check the 'Evaluation' and 'Result' columns in step 2 of the execution_table
At which step is the variable p assigned an actual instance of Person?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in the execution_table for when p is assigned
If we remove 'typeof' from InstanceType<typeof Person>, what happens?
AIt causes a TypeScript error
BIt still works the same
CIt changes p to be a string
DIt makes p any type
💡 Hint
Refer to key_moments question 1 and execution_table step 2
Concept Snapshot
InstanceType<Type> extracts the instance type from a class type.
Use typeof ClassName to get the class type.
Example: type T = InstanceType<typeof MyClass>;
T is the type of new MyClass().
Useful for typing variables or functions expecting class instances.
Full Transcript
This visual execution trace shows how the TypeScript utility type InstanceType works. First, we define a class Person with a property name. Then, we use InstanceType with typeof Person to get the instance type of the class. This instance type is assigned to PersonInstance. Next, we declare a variable p of type PersonInstance and assign it a new Person instance. We see that p can access the name property safely. Key points include using typeof to get the class type. The quizzes test understanding of these steps and common confusions.

Practice

(1/5)
1. What does the TypeScript utility type InstanceType do?
easy
A. Creates a new class instance
B. Extracts the type of an instance created by a class
C. Defines a type alias for primitive types
D. Converts a type to a string

Solution

  1. Step 1: Understand the purpose of InstanceType

    InstanceType is a utility type that extracts the type of an object created by a class constructor.
  2. Step 2: Compare options with the definition

    Only Extracts the type of an instance created by a class correctly describes this behavior. Options B, C, and D describe unrelated actions.
  3. Final Answer:

    Extracts the type of an instance created by a class -> Option B
  4. Quick Check:

    InstanceType extracts instance type = A [OK]
Hint: Remember: InstanceType gets the object type from a class [OK]
Common Mistakes:
  • Confusing InstanceType with creating instances
  • Thinking it converts types to strings
  • Assuming it defines primitive types
2. Which of the following is the correct syntax to use InstanceType with a class named Car?
easy
A. type CarInstance = InstanceType;
B. type CarInstance = InstanceType;
C. type CarInstance = InstanceType;
D. type CarInstance = InstanceType<Car>;

Solution

  1. Step 1: Recall the syntax of InstanceType

    InstanceType expects a constructor type, so we pass typeof Car to get the class constructor type.
  2. Step 2: Analyze each option

    type CarInstance = InstanceType; uses typeof Car, which is correct. type CarInstance = InstanceType<Car>; passes the class itself, which is a value, not a type. Options A, B, and C use invalid syntax.
  3. Final Answer:

    type CarInstance = InstanceType; -> Option A
  4. Quick Check:

    Use typeof with class name inside InstanceType for correct syntax = D [OK]
Hint: Use typeof ClassName inside InstanceType for correct syntax [OK]
Common Mistakes:
  • Passing class name directly without typeof
  • Using parentheses after class name
  • Trying to instantiate inside InstanceType
3. Given the code below, what is the type of myDog?
class Dog {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

let myDog: InstanceType;
myDog = new Dog('Buddy');
medium
A. string
B. { name: string; }
C. typeof Dog
D. Dog

Solution

  1. Step 1: Understand what InstanceType means

    It extracts the instance type created by the Dog class constructor, which is the class Dog itself.
  2. Step 2: Match the type of myDog

    myDog is typed as an instance of Dog, so its type is Dog (the class instance type). Dog matches this exactly.
  3. Final Answer:

    Dog -> Option D
  4. Quick Check:

    InstanceType = Dog instance type = A [OK]
Hint: InstanceType equals the class instance type [OK]
Common Mistakes:
  • Confusing instance type with constructor type
  • Choosing typeof Class instead of instance
  • Picking object shape instead of class type
4. Identify the error in the following TypeScript code using InstanceType:
class Cat {
  age: number;
  constructor(age: number) {
    this.age = age;
  }
}

type CatInstance = InstanceType<Cat>;

const kitty: CatInstance = new Cat(3);
medium
A. Missing typeof before Cat in InstanceType
B. Cannot assign new Cat(3) to kitty
C. Cat class must have a method
D. InstanceType cannot be used with classes

Solution

  1. Step 1: Check the usage of InstanceType

    InstanceType requires a constructor type, so the class name must be prefixed with typeof.
  2. Step 2: Identify the error in the code

    The code uses InstanceType<Cat> instead of InstanceType, causing a type error.
  3. Final Answer:

    Missing typeof before Cat in InstanceType -> Option A
  4. Quick Check:

    InstanceType needs typeof ClassName = C [OK]
Hint: Always use typeof with class name inside InstanceType [OK]
Common Mistakes:
  • Omitting typeof before class name
  • Thinking InstanceType works on instances
  • Assuming InstanceType works without constructor type
5. You have two classes, Circle and Square. You want to create a function that accepts an instance of either class using InstanceType. Which of the following is the correct way to type the parameter shape?
class Circle {
  radius: number;
  constructor(radius: number) {
    this.radius = radius;
  }
}

class Square {
  side: number;
  constructor(side: number) {
    this.side = side;
  }
}

function drawShape(shape: ???) {
  // drawing logic
}
hard
A. Circle | Square
B. typeof Circle | typeof Square
C. InstanceType | InstanceType
D. InstanceType

Solution

  1. Step 1: Understand the goal

    The function should accept instances of Circle or Square, so the parameter type must represent instances, not classes or constructors.
  2. Step 2: Analyze each option

    InstanceType | InstanceType correctly uses InstanceType and InstanceType combined with a union to represent instances of both classes. Circle | Square uses class names directly, which are types of instances but does not use InstanceType. typeof Circle | typeof Square uses constructor types, which is incorrect for instances. InstanceType tries to use InstanceType on a union of classes, which is invalid.
  3. Final Answer:

    InstanceType | InstanceType -> Option C
  4. Quick Check:

    Use InstanceType with typeof for each class, combine with | for union [OK]
Hint: Use InstanceType for each, join with | for multiple classes [OK]
Common Mistakes:
  • Using class names without InstanceType
  • Using typeof Class instead of InstanceType
  • Trying InstanceType on union of classes directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard