TypeScript Questions with Answers

8 practice questions on TypeScript (React & Web) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What does the? mean in interface User { name: string; age?: number }?

  1. age is private and only visible inside the interface
  2. age is read-only and cannot be reassigned later
  3. age may be null, but it must always be present
  4. age is optional, so its type is number | undefined

Answer: D. age is optional, so its type is number | undefined

Explanation

An optional property can be omitted when creating the object, and reading it gives number | undefined.

Step-by-step solution

  1. The question mark after age in an interface marks the property as optional.
  2. An optional property may be left out when an object is created.
  3. Reading it gives number | undefined, so its type is number or undefined, which is option D.

Why the other options are wrong

  • A. The question mark does not make a property private; TypeScript interfaces have no private members.
  • B. Read-only properties use the readonly keyword, not a question mark.
  • C. An optional property can be missing; it is not required to be present, and null is not implied.

Tip: Name? means the property may be missing, and its type includes undefined.

Question 2Easy

Are TypeScript type annotations checked while the program runs?

  1. No, types are erased during compilation and only checked at compile time
  2. Only in strict mode
  3. Yes, types are enforced at runtime
  4. Only interfaces are checked at runtime

Answer: A. No, types are erased during compilation and only checked at compile time

Explanation

TypeScript compiles to plain JavaScript without types. Use runtime validation (for example a schema library) for data from outside, such as API responses.

Step-by-step solution

  1. TypeScript checks types while it compiles the code.
  2. The compiler removes the type annotations, and the emitted JavaScript has no type checks.
  3. So types are not checked at runtime, which is option A.

Why the other options are wrong

  • B. Strict mode changes what the compiler reports, but types are still erased at runtime.
  • C. Types are not enforced at runtime; the JavaScript that runs has no type checks.
  • D. Interfaces are removed during compilation, so none are checked at runtime.

Tip: Validate data from outside (such as an API response) at runtime, because TypeScript types are erased.

Question 3Medium

Under strict mode, what happens when this compiles?

let value: unknown = "hello";
console.log(value.toUpperCase());
  1. It compiles and logs HELLO
  2. Compile error: value is of type unknown and must be narrowed first
  3. It compiles, but value is converted to any
  4. Runtime error only

Answer: B. Compile error: value is of type unknown and must be narrowed first

Explanation

unknown is the type-safe counterpart of any: you must narrow it (for example with typeof value === "string") before using it. With any the call would compile.

Step-by-step solution

  1. Value is declared as unknown, so TypeScript does not know it is a string.
  2. Calling toUpperCase on an unknown value is not allowed without narrowing it first.
  3. Under strict mode this is a compile error: value must be narrowed, for example with typeof, which is option B.

Why the other options are wrong

  • A. The code does not compile, so it does not log HELLO.
  • C. Unknown is not converted to any; the method call is still an error.
  • D. The error is caught by the compiler before the program runs.

Tip: Use unknown for values of unclear type and narrow them with typeof or instanceof before use.

Question 4Medium

Given interface User { id: number; name: string; }, what is Partial<User>?

  1. { id: number }
  2. { id: number | null; name: string | null }
  3. An array of User
  4. { id?: number; name?: string }

Answer: D. { id?: number; name?: string }

Explanation

Partial<T> makes every property optional. It is useful for update payloads.

Step-by-step solution

  1. The Partial utility type makes every property of the type optional.
  2. Applied to User, the properties id and name both get a question mark.
  3. So the Partial version of User is the object type with id optional and name optional, which is option D.

Why the other options are wrong

  • A. This keeps id required and drops name, so it is not Partial.
  • B. Partial does not add null; it makes properties optional.
  • C. Partial maps the properties of the object type; it does not make an array.

Tip: Partial is useful for update payloads, where only some fields are sent.

Question 5Medium

Given interface User { id: number; name: string; email: string; }, which type is { id: number; name: string }?

  1. Pick<User, "id" | "name">
  2. Omit<User, "id" | "name">
  3. Partial<User>
  4. Record<"id" | "name", User>

Answer: A. Pick<User, "id" | "name">

Explanation

Pick keeps the listed keys. Omit<User, "email"> would give the same result.

Step-by-step solution

  1. The required type has only id and name, and drops email.
  2. Pick with "id" | "name" keeps exactly the listed keys.
  3. So Pick with "id" | "name" gives { id: number; name: string }, which is option A.

Why the other options are wrong

  • B. Omit<User, "id" | "name"> keeps only email, which is the opposite.
  • C. Partial<User> makes all three fields optional, which is not the required type.
  • D. Record<"id" | "name", User> gives an object whose values are User, not the field types.

Tip: Pick keeps the keys you list; Omit removes the keys you list.

Question 6Medium

What does this function signature describe?

function first<T>(items: T[]): T | undefined {
  return items[0];
}
  1. A function that returns an array
  2. A generic function: the return type matches the element type of the array passed in
  3. A function that only accepts arrays of any
  4. A class constructor

Answer: B. A generic function: the return type matches the element type of the array passed in

Explanation

T is a type parameter inferred from the argument: first([1, 2]) returns number | undefined, and first(["a"]) returns string | undefined.

Step-by-step solution

  1. T is a type parameter, which TypeScript fills in from the argument.
  2. Items is an array of T, and the function returns T or undefined.
  3. So the return type follows the element type of the array passed in, which is option B.

Why the other options are wrong

  • A. The function returns an element, not an array.
  • C. It is generic, so T is inferred from the argument; it is not limited to any.
  • D. No class or new keyword is involved; this is a plain function.

Tip: Generics let one function keep the type of its input in its output.

Question 7Hard

What is the type of colors?

const colors = ["red", "green"] as const;
  1. ("red" | "green")[]
  2. string[]
  3. readonly ["red", "green"]
  4. ["red", "green"]

Answer: C. readonly ["red", "green"]

Explanation

as const makes the array a readonly tuple and keeps the literal types of its elements.

Step-by-step solution

  1. As const makes the array a readonly tuple.
  2. It keeps the exact literal values for each element, "red" and "green".
  3. So the type is readonly ["red", "green"], which is option C.

Why the other options are wrong

  • A. A plain array of the union type is what you get without as const; as const keeps the tuple.
  • B. The elements keep their literal types, so they are not widened to string.
  • D. The tuple is readonly with as const, so the mutable form in option D is wrong.

Tip: Use as const to get a readonly tuple with literal element types.

Question 8Hard

In the default branch of this switch, what type does shape have, and why is the assignment useful?

type Shape = { kind: "circle"; r: number } | { kind: "square"; s: number };

function area(shape: Shape): number {
  switch (shape.kind) {
    case "circle": return Math.PI * shape.r ** 2;
    case "square": return shape.s ** 2;
    default:
      const unreachable: never = shape;
      return unreachable;
  }
}
  1. any; it allows any value
  2. unknown; it disables type checking
  3. never; if a new kind is added to Shape and not handled, this line becomes a compile error
  4. Shape; it converts the value

Answer: C. never; if a new kind is added to Shape and not handled, this line becomes a compile error

Explanation

After all cases are handled, narrowing leaves nothing, so shape is never. Adding a new union member makes the assignment fail to compile, which flags the missing case.

Step-by-step solution

  1. The switch covers the "circle" and "square" cases of Shape.
  2. In the default branch, TypeScript narrows shape to nothing, so its type is never.
  3. If a new kind is added to Shape and not handled, the never assignment fails to compile.
  4. So the type is never, which is option C.

Why the other options are wrong

  • A. Any would allow any value and would hide the missing case.
  • B. Unknown disables the check that the assignment gives.
  • D. Shape would still compile after a new kind is added, so the check would not catch it.

Tip: Assign the leftover value to never in a default branch to catch unhandled union members at compile time.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all React & Web topics · All topics