TypeScript Refactoring Interview Questions
Briefly

TypeScript Refactoring Interview Questions
"A good TypeScript developer does not only ask: Does this compile? They also ask: Can this represent invalid data? Will this still be safe after the next refactor? Does this type describe the real domain? Is the compiler helping us, or are we fighting it? Will another developer understand this in six months?"
"TypeScript inferred a type that is too broad. How would you fix it, and why does it matter? This is a great question because it tests whether the candidate understands inference, literal types, generics, and practical safety. Here is a weak version: const ageByUsername = new Map() ageByUsername.set('nina', 31) ageByUsername.set('mark', 'unknown')"
"TypeScript infers: Map That means the map accepts anything as a key and anything as a value. The compiler is no longer protecting the code. A safer version: const ageByUsername = new Map() ageByUsername.set('nina', 31) ageByUsername.set('mark', 42) // Error: // Argument of type 'string' is not assignable to parameter of type 'number'. ageByUsername.set('alex', 'unknown')"
"import { useState } from 'react' const [paymentStatus, setPaymentStatus] = useState('idle') At first glance, this looks fine. But in many cases TypeScript may infer a broad string, depending on how the state is used. That allows invalid states: setPaymentStatus('banana') setPaymentStatus('something-went-wrong')"
TypeScript interviews are shifting from syntax questions to evaluating whether developers can spot fragility in code that compiles. Strong candidates check whether types prevent invalid data, remain safe after refactors, accurately model the domain, and help the compiler enforce correctness rather than fight it. They also consider long-term readability for other developers. A common scenario involves overly broad inferred types, such as a Map inferred to accept any key and any value when initialized without explicit typing. Fixing it by constraining the inferred types makes incorrect values fail during development and communicates intent. Similar issues arise with React state when a broad string is inferred, allowing invalid state updates unless the state is constrained to a union of allowed values.
Read at reactdevelopment.substack.com
Unable to calculate read time
[
|
]