Skip to content
DevFlash

By technology

TypeScript interview questions: types, narrowing and generics

Type modeling and the limits of static checking, with short examples compatible with TypeScript 5.

  • 5 min read
  • 6 sections
  • Updated November 2, 2026

This guide is under editorial review.

In 1 minute

  • Explain the relationship between input, operation and output, and adapt types when the contract changes.
  • Use unions for explicit alternatives and generics when they preserve a useful relationship between types.
  • Types and assertions do not validate external data; check shape and separate business rules from authorization.

TypeScript interview questions test whether you can represent rules and explain where static checking ends. Writing a type does not prove an API response follows that shape. Silencing the compiler is not enough either: an assertion can hide the uncertainty you needed to resolve. The goal is to make assumptions and valid states explicit.

The examples use features available in TypeScript 5 without relying on elaborate types to impress. Start with the business rule, then show its representation and an invalid use it would prevent. If the question concerns runtime behavior, consult the JavaScript questions. This distinction avoids crediting the type system with behavior it never executes.

Answering TypeScript interview questions

  1. Explain the contract

    Explain the relationship between input, operation and output.

  2. Delimit the identifier

    For a function receiving an identifier, say whether any string is sufficient or the domain requires a format. A UserId alias documents intent, but a plain string alias does not create a distinct nominal identity.

  3. Show validation

    When a guarantee depends on validation, show where validation occurs.

  • Show a valid call and one that should be rejected.
  • Distinguish compile-time errors from runtime failures.
  • State relevant checking options, such as strictNullChecks.
  • Prefer a readable model that represents the rule without duplication.

Follow-up questions may change a condition: a field can be missing, an operation can fail or a list can be empty. Adjust the model to that change. Do not defend a type simply because it compiled in the original example.

Inference, annotations and structural typing

Inference lets the compiler determine types from values and context. Use annotations to expose contracts when that helps callers; repeating the type of every literal is unnecessary. A let variable may receive a broader type because its value can change. Compare that with a constant before concluding that inference lost information.

Structural typing considers value shapes. A function that needs id can accept an object with additional fields when the assignment is compatible. Object literals receive excess-property checks in specific contexts; this does not make all types exact contracts. Explain the difference between a directly supplied object literal and an existing variable.

Unions, narrowing and interface states

Three booleans such as loading, failed and ready allow combinations that may not make sense. A discriminated union represents explicit alternatives. Here, the identifier exists only for completed work, and the message exists only for failure. Checking status narrows the possible value shapes. That is control-flow narrowing, not object conversion.

type Job =
| { status: "pending" }
| { status: "done"; id: string }
| { status: "failed"; message: string };
function describeJob(job: Job): string {
switch (job.status) {
case "pending": return "Waiting";
case "done": return `Completed: ${job.id}`;
case "failed": return job.message;
}
}

When adding a state, review every consumer. Exhaustive handling with never can help locate missed cases. Still, the type represents states allowed by your model; it does not automatically verify that transitions obey the process. For components, connect this modeling to the React questions.

Generics and input-output relationships

A generic is useful when it preserves a meaningful relationship between types. This function returns an element of the input list's type, or undefined for an empty list. Returning unknown would erase that relationship; listing every imaginable type in a union would be hard to maintain. The type parameter expresses variation without losing information.

function first<T>(items: readonly T[]): T | undefined {
return items[0];
}
const item = first([{ id: "a", price: 20 }]);
if (item !== undefined) {
console.log(item.price);
}

Do

  • If a function only handles orders, a concrete order type may be clearer.
  • When accessing a property, add a constraint that represents it.

Avoid

  • Adding type parameters that appear once without connecting meaningful information.
  • Building an abstraction that accepts everything and relies on assertions to perform its actual work.

any, unknown and external-data validation

any allows operations without the usual checking protection, and mistakes can spread into derived values. unknown accepts an unclassified value but requires narrowing before using it. At an API boundary, that forces the right question: which properties have actually been checked? A response annotation does not inspect received JSON.

type Profile = { name: string };
function isProfile(value: unknown): value is Profile {
return typeof value === "object"
&& value !== null
&& "name" in value
&& typeof value.name === "string";
}

This guard checks only the minimal shape shown. It does not require a nonempty name, strip extra fields or validate permissions. An incorrect predicate can also mislead the compiler, so verify its implementation. For larger contracts, a schema library can reduce repetition, provided you understand its criteria and reported errors.

Different shapes entering a gate that allows only one shape type through.
Static checking describes contracts; external data needs runtime validation to verify the received shape.

satisfies, as const and assertion limits

satisfies checks an expression against a type while retaining useful inferred information instead of simply replacing it with the broad contract. as const preserves literals and infers readonly properties and tuples. It does not freeze an object at runtime. Shared mutable references still deserve attention: readonly is a static restriction, not a security boundary.

An as assertion asks the compiler to accept an interpretation; it neither transforms nor validates the value. The same caution applies to non-null assertions. Before using one, explain the external evidence behind the guarantee. Review contracts with front-end tests, and remember that typed attributes do not establish interface accessibility.

Frequently asked questions

What TypeScript topics should I study for interviews?

Prioritize inference, unions, narrowing, generics and external data. Model states and explain which mistakes the compiler catches, then show a case requiring runtime validation.

What is the difference between any and unknown?

any permits operations without usual checks. unknown requires narrowing before use. For data whose structure has not been verified, unknown keeps that uncertainty visible.

When should I use generics instead of a concrete type?

Use them when an operation preserves a relationship across variable types, such as returning an input list element. If the rule belongs to one entity, prefer a readable concrete contract.

Does TypeScript validate data received from an API?

No. Types do not inspect responses at runtime. Validate data with explicit checks or a schema before trusting it, including any necessary business rules.

Practice with real tests

Solve front-end exercises in the browser, with the same tests a reviewer would run.

See the exercises