Skip to content

Web · updated 26 November 2026

TypeScript adds static types to JavaScript. These scenarios test type safety at boundaries, narrowing, generics and migration.

Top 10 TypeScript scenario questions

Try each one yourself first, then open the model answer.

1. An API response typed with an interface still crashes at runtime with a missing field. Why?

Show answer

Types are erased at compile time and a type assertion does not check data. Validate external input at runtime, for example with a schema library, and derive the type from the schema.

2. A teammate uses `any` to silence errors. What would you suggest?

Show answer

any switches checking off and spreads. Use unknown for values of uncertain shape and narrow before use, and enable strict mode with a lint rule against explicit any.

3. You add a new variant to a union and some switch statements silently ignore it. How do you catch this?

Show answer

Add a default branch that assigns the value to a variable of type never. The compiler then reports every switch that does not handle the new variant.

4. A state object can be loading, success or error, and code reads data while loading. How would you model it?

Show answer

Use a discriminated union with a status field, where data exists only on the success variant. The compiler then requires a status check before access.

5. You need a function that returns the same type it receives, whatever that is. What do you write?

Show answer

A generic function, with a type parameter used for both the argument and the return value. Add a constraint with extends if the function needs particular properties.

6. You want a type-checked config object but also keep its exact literal types. What would you use?

Show answer

The satisfies operator checks the value against a type without widening it. Combined with as const it preserves literal values.

7. You must migrate a large JavaScript codebase without stopping feature work. How?

Show answer

Enable allowJs, convert files gradually starting with shared and high-risk modules, add types at boundaries first, and tighten strictness step by step with CI preventing new errors.

8. Two IDs, UserId and OrderId, are both strings and get mixed up. Can the compiler help?

Show answer

TypeScript is structurally typed, so aliases are interchangeable. Use branded types, adding a unique marker property to each, and create values through validating functions.

9. A third-party library has no types. What do you do?

Show answer

Check for a community types package, otherwise write a small declaration file covering only what you use, and keep unknown for uncertain values.

10. Builds have become very slow in a large monorepo. What would you try?

Show answer

Use project references and incremental builds, enable skipLibCheck where acceptable, separate type checking from transpiling, and look for very complex recursive types.

Ready to build something great?

Book your free 30-minute consultation today. No obligation, just honest advice.