Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

What do InstanceType, ConstructorParameters, and the this utility types do?

Questions in this chapter (8)
  • Draft
  • medium
  • Askedsometimes
  • Verified October 2026

Answer

All five are declared in lib.es5.d.ts; four are conditional types using infer.

Class utilities. A class name used as a type means the instance; the class value's type is typeof C, which has a construct signature. ConstructorParameters<typeof C> infers that signature's parameters as a labelled tuple (constructor(x: number, y = 0) gives [x: number, y?: number | undefined]), and InstanceType<typeof C> infers what new returns. Both are constrained to abstract new (...args: any) => any, so they also accept abstract classes (TS 4.2 abstract construct signatures). They are what you reach for when a value is a class: factories, DI containers, registries keyed by constructor.

They work on one signature and they don't keep generics. An overloaded constructor gives the parameters of the last overload, and InstanceType<typeof Box> for class Box<T> is Box<unknown>. When writing a generic factory, inferring new (...args: A) => T directly is usually better, because T and A are inferred per call.

this utilities. A function can declare a fake first parameter this: X. ThisParameterType<F> extracts X (or unknown if there is none), and OmitThisParameter<F> returns F without it. Their main client is the lib itself: with strictBindCallApply (part of --strict), bind is declared as bind<T>(this: T, thisArg: ThisParameterType<T>): OmitThisParameter<T>. OmitThisParameter rebuilds the function type from the last overload, so generics become unknown.

ThisType<T> is an empty interface the checker treats as a marker: in an object literal whose contextual type includes ThisType<T>, methods see this: T. It powers "options object" APIs in which methods reach data defined elsewhere in the same object. It requires noImplicitThis (on under --strict); without that flag this is just any.

Example

// TypeScript 7.0.2 and 6.0.3, --strict; run on Node 24.19
class Box<T> { constructor(public value: T) {} }

// A factory built from the two class utilities...
function make<C extends new (...args: any) => any>(
  ctor: C, ...args: ConstructorParameters<C>
): InstanceType<C> {
  return new ctor(...args);
}
const loose = make(Box, "hi");   // Box<unknown>: the class's own generic is lost
// ...versus inferring the parts directly, which keeps it
function create<A extends unknown[], T>(ctor: new (...args: A) => T, ...args: A): T {
  return new ctor(...args);
}
const tight = create(Box, "hi"); // Box<string>
console.log(tight.value.toUpperCase()); // → HI

// The `this` parameter utilities type Function.prototype.bind
function toHex(this: number, pad: number) { return this.toString(16).padStart(pad, "0"); }
type Self = ThisParameterType<typeof toHex>; // number
const ffHex = toHex.bind(255);               // OmitThisParameter<typeof toHex>: (pad: number) => string
console.log(ffHex(4)); // → 00ff

// ThisType<T> only marks what `this` is inside an object literal
type Options<D, M> = { data: D; methods: M & ThisType<D & M> };
function define<D, M>(o: Options<D, M>): D & M { return { ...o.data, ...o.methods }; }
const counter = define({ data: { count: 0 }, methods: { inc() { this.count++; } } });
counter.inc();
console.log(counter.count); // → 1

Pitfalls

  • Passing the instance type. InstanceType<Point> is TS2344: "Type 'Point' does not satisfy the constraint 'abstract new (...args: any) => any'". Write InstanceType<typeof Point>.
  • Private constructors. InstanceType<typeof Singleton> is TS2344 too ("Cannot assign a 'private' constructor type to a 'public' constructor type"), because the constraint is a public construct signature.
  • Trusting them with overloads or generics: only the last overload survives, and type parameters become unknown.
  • Relying on ThisType with noImplicitThis off. The marker is ignored and this.missing type-checks silently; with --strict it is TS2339.

Follow-ups

Why does the constraint say (...args: any) rather than unknown[]?
Parameters are compared contravariantly, so typeof Point (taking number) is not assignable to new (...args: unknown[]) => unknown: TS2345 on both versions. 7.0.2 elaborates "Types of parameters 'x' and 'args' are incompatible", 6.0.3 "Types of construct signatures are incompatible". any or never[] accepts every constructor.
How is this different from a this return type?
add(p: string): this in a class is polymorphic this: called on a subclass, it returns the subclass, so fluent chains keep subclass methods. ThisType sets this inside object literals; the this parameter utilities read a function's declared receiver.
What does calling toHex(2) directly do?
TS2684: "The 'this' context of type 'void' is not assignable to method's 'this' of type 'number'". The this parameter is checked at every call site and erased from the emitted JavaScript.

Exercises

Predict the types

class Over {
  constructor(a: string);
  constructor(a: number, b: number);
  constructor(..._: unknown[]) {}
}
type P = ConstructorParameters<typeof Over>;
function id<T>(this: Date, x: T) { return x; }
type O = OmitThisParameter<typeof id>;

What is `ThisParameterType<(a: number) => void>`?

type T = ThisParameterType<(a: number) => void>;

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.