How To Use the Built-In InstanceType Type in TypeScript
Sailing the instances of TypeScript classes with mapaches š¦
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!