How To Use the Built-In InstanceType Type in TypeScript

Sailing the instances of TypeScript classes with mapaches šŸ¦

Dr. Derek Austin šŸ„³
Totally TypeScript
Published in
3 min readDec 27, 2023

--

You see, these glasses are an instance of the Glasses class. Woof, woof. Photo by Jamie Street on Unsplash

TypeScript, ahoy! šŸ“ā€ā˜ ļø The sea spray, the mapacheā€™s treasure trove, the lo-fi radio! šŸ¦œOur ship is full to the brim of nifty tools to make our developer lives more type-safe and predictable. Letā€™s open our treasure chest, and what will we find? Today, itā€™s an OOP jewel: the built-in InstanceType type.

What is InstanceType in TypeScript?

First off, letā€™s shed some light on what InstanceType is. This built-in type allows you to obtain an instance type of a class or constructor function.

In other words, it gives you a way to obtain the type of an object instance without explicitly declaring it.

Think of a mapache šŸ¦ examining an object. Itā€™s less about the objectā€™s origin and more about the shape and features of the object itself.

Thatā€™s the best mapache metaphor I could come up with. I recently wrote on TypeScript / JavaScript classes and constructor functions if you want to read the companion piece to this article before moving on. No obligation!

Getting the InstanceType of Mapache Class

--

--

Dr. Derek Austin šŸ„³
Totally TypeScript

I write about real-world programming career advice, MongoDB vs. PostgreSQL, Git, React, JavaScript, VS Code, TypeScript, and Next.js. Doctor of Physical Therapy