Bird
Raised Fist0
Typescriptprogramming~20 mins

InstanceType type in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
InstanceType Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code using InstanceType?
Consider the following TypeScript code using InstanceType. What will be logged to the console?
Typescript
class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  speak() {
    return `${this.name} makes a noise.`;
  }
}

type AnimalInstance = InstanceType<typeof Animal>;

const pet: AnimalInstance = new Animal('Dog');
console.log(pet.speak());
A"Dog makes a noise."
B"Animal makes a noise."
CTypeError at runtime
DCompilation error due to InstanceType misuse
Attempts:
2 left
💡 Hint
InstanceType extracts the instance type from a constructor function type.
🧠 Conceptual
intermediate
1:30remaining
What does InstanceType do in TypeScript?
Choose the best description of what InstanceType<T> does when T is a constructor type.
AConverts a class type to a primitive type.
BExtracts the type of the constructor function itself, not the instance.
CCreates a new instance of type <code>T</code> at runtime.
DExtracts the type of the instance created by the constructor type <code>T</code>.
Attempts:
2 left
💡 Hint
Think about what a constructor function returns when called with new.
❓ Predict Output
advanced
2:00remaining
What is the type of variable 'obj' here?
Given this TypeScript code, what is the type of obj?
Typescript
class Person {
  constructor(public name: string, public age: number) {}
}

type PersonConstructor = typeof Person;

type PersonInstance = InstanceType<PersonConstructor>;

const obj: PersonInstance = { name: 'Alice', age: 30 };
AAn instance of <code>Person</code> with methods only, no properties.
BA constructor function type, not an object.
CAn object with properties <code>name</code> (string) and <code>age</code> (number).
DA compilation error because <code>obj</code> is not created with <code>new</code>.
Attempts:
2 left
💡 Hint
InstanceType extracts the instance type, which includes properties.
🔧 Debug
advanced
1:30remaining
What error does this code produce?
Examine this TypeScript code using InstanceType. What error will the compiler show?
Typescript
type NotAConstructor = {
  foo: string;
};

type Result = InstanceType<NotAConstructor>;
AType 'NotAConstructor' does not satisfy the constraint 'new (...args: any) => any'.
BNo error, Result is type 'any'.
CRuntime error when accessing Result.
DSyntax error due to missing constructor.
Attempts:
2 left
💡 Hint
InstanceType requires a constructor type as input.
🧠 Conceptual
expert
2:30remaining
Why use InstanceType instead of directly using a class type?
Which reason best explains why you might use InstanceType<typeof SomeClass> instead of just using SomeClass as a type?
ATo create a new instance of the class at runtime automatically.
BTo get the instance type from a constructor type when the class type is not directly accessible or to work with constructor signatures generically.
CTo convert the class type into a primitive type for optimization.
DTo enforce that the class has no static methods.
Attempts:
2 left
💡 Hint
Think about generic programming and working with constructor types.

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