Create a generic State class with getState and setState
Binding a generic class parameter so methods share one consistent type.
Write class State<T> with private T, constructor(T), getState(): T, and setState(T).
Using any instead of T, erasing type safety.
WHAT THIS TESTS: This question evaluates whether you understand how to lift a generic type parameter to the class declaration so that every method and property shares the same concrete type after instantiation. It is not about memorizing syntax; it is about knowing that generics let you traffic type information through the entire lifecycle of an object. The interviewer wants to see that you treat the type parameter as a contract established once in the constructor and honored by every subsequent method.
A GOOD ANSWER COVERS: First, declare the class with a type parameter, for example class State<T>. Second, add a private field that holds the current value and is typed as T. Third, write a constructor that accepts an argument of type T and assigns it to the field. Fourth, implement getState with a return type of T so callers retrieve the exact type they provided. Fifth, implement setState with a parameter of type T so updates remain consistent with the original contract. Mentioning that TypeScript will infer the concrete type when you instantiate the class with new State(0) shows fluency.
COMMON WRONG ANSWERS: Using any for the field or method signatures erases type information and makes the generic pointless. Declaring separate generic parameters on each method instead of one at the class level breaks the connection between them, allowing getState and setState to disagree on the type. Forgetting to type the constructor argument leads to implicit any or unexpected inference. Another red flag is returning unknown and forcing the caller to cast, which shifts type safety burdens to the consumer.
LIKELY FOLLOW-UPS: The interviewer may ask how you would add a subscribe method that accepts a callback typed as (newState: T) => void to build a simple observer pattern. They might ask how to constrain T to extend object or a specific interface. You could also be asked to make the class immutable by returning a new State instance from setState rather than mutating the internal field, or to discuss variance and whether T should be in an invariant position.
ONE CONCRETE EXAMPLE: A solid implementation looks like this in code structure. class State<T> { private state: T; constructor(initialState: T) { this.state = initialState; } getState(): T { return this.state; } setState(newState: T): void { this.state = newState; } } When you write const counter = new State(0), TypeScript infers counter as State<number>. Calling counter.setState('1') then produces a compile-time error because string is not assignable to number, which is exactly the behavior the question is designed to produce.
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.