TypeScript Narrowing: A Small Example beyond Using any
A TypeScript value can have more than one possible type. Narrowing means using a check that lets TypeScript understand which possibility applies…
Suppose a React screen receives a first name and a last name. It needs to display the full name. You do not normally need another state value and an Effect just to combine those two strings.
function PersonName({ firstName, lastName }) {
const fullName = `${firstName} ${lastName}`.trim();
return <span>{fullName}</span>;
}
The displayed value is calculated from the current props during rendering. There is no separate full-name state to keep in sync. React’s official guide explains that Effects are for synchronising with external systems, not usually for transforming data for display.
If you store the combined name separately and update it in an Effect, you add another update path and an extra render. For a simple calculation, that makes the component harder to follow without adding useful behaviour. This does not mean all Effects are wrong; the purpose of the code matters.
Take a small list of products and a search term. First write down what should be displayed when the term is empty, matches one item or matches nothing. Then calculate the filtered list from the current inputs. Keep the example free of network requests so you can focus on the display logic.
Before adding state, ask: can this value be calculated from information the component already has? Explain your answer with the example. That gives a Malta frontend interviewer a clear view of your reasoning without relying on framework buzzwords.
Browse current technology vacancies with Huwell Consultancy and ask us about the responsibilities that matter to you.
Explore opportunitiesA TypeScript value can have more than one possible type. Narrowing means using a check that lets TypeScript understand which possibility applies…
A .NET vacancy may mention several tools, but your interview example should still be easy to follow. Choose one service you know…
Frontend testing is easier to discuss when you connect it to a feature. Before a developer interview in Malta, choose one change…