What is the polymorphic this type in TypeScript?
This tests polymorphic this for type-safe fluent APIs. A strong answer defines this as the current instance type, implements CSSBuilder methods that return this for chaining, and notes subclass preservation.
WHAT THIS TESTS: This question probes your understanding of TypeScript's polymorphic this type and its role in maintaining accurate types across class hierarchies, particularly in fluent builder APIs. Interviewers want to see that you know the difference between the static class name and the dynamic this type, and that you can leverage it to prevent type information loss when subclasses extend a base builder. They also care whether you understand why this matters for library design and developer experience.
A GOOD ANSWER COVERS: First, define polymorphic this as a special type that refers to the type of the current instance rather than the class where the method is declared. Second, explain that when a method returns this, TypeScript preserves the exact derived type through the chain, so a subclass like ExtendedCSSBuilder stays typed as ExtendedCSSBuilder after calling inherited methods. Third, sketch the CSSBuilder implementation with methods like setColor and setMargin each returning this, allowing sequential calls such as new CSSBuilder().setColor('red').setMargin('10px'). Fourth, mention that this pattern is superior to returning the base class name because it enables subclassing without re-declaring methods just to fix return types. Finally, note that polymorphic this works because TypeScript treats this in a class as a type parameter under the hood.
COMMON WRONG ANSWERS: Returning CSSBuilder explicitly from every method instead of this; this forces all chains to collapse to the base type and discards subclass properties. Another red flag is confusing this with generics like return type T extends CSSBuilder; while generics can work, they are verbose and unnecessary when polymorphic this is built for exactly this scenario. Also, saying this is just a JavaScript runtime concept without acknowledging its TypeScript type-level behavior shows shallow knowledge.
LIKELY FOLLOW-UPS: The interviewer might ask how polymorphic this interacts with strict function types or how you would type a base class factory method that returns an instance of the derived class. They could also ask you to compare polymorphic this against using abstract classes with generic type parameters, or how to handle a method that conditionally returns this versus void.
ONE CONCRETE EXAMPLE: Consider a base class CSSBuilder with a private styles map. The method setColor receives a string and returns this; setMargin receives a string and returns this. If you declare class ResponsiveCSSBuilder extends CSSBuilder and add a setBreakpoint method, then calling new ResponsiveCSSBuilder().setColor('blue').setBreakpoint('768px') type-checks only because setColor returned this, preserving the ResponsiveCSSBuilder type. Had setColor returned CSSBuilder, the subsequent setBreakpoint call would error since CSSBuilder lacks that method.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.