Sample questions
Question 1Easy
What does the? mean in interface User { name: string; age?: number }?
- age is private and only visible inside the interface
- age is read-only and cannot be reassigned later
- age may be null, but it must always be present
- 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
- The question mark after age in an interface marks the property as optional.
- An optional property may be left out when an object is created.
- 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?
- No, types are erased during compilation and only checked at compile time
- Only in strict mode
- Yes, types are enforced at runtime
- 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
- TypeScript checks types while it compiles the code.
- The compiler removes the type annotations, and the emitted JavaScript has no type checks.
- 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());
- It compiles and logs HELLO
- Compile error: value is of type unknown and must be narrowed first
- It compiles, but value is converted to any
- 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
- Value is declared as unknown, so TypeScript does not know it is a string.
- Calling toUpperCase on an unknown value is not allowed without narrowing it first.
- 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>?
- { id: number }
- { id: number | null; name: string | null }
- An array of User
- { 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
- The Partial utility type makes every property of the type optional.
- Applied to User, the properties id and name both get a question mark.
- 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 }?
- Pick<User, "id" | "name">
- Omit<User, "id" | "name">
- Partial<User>
- 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
- The required type has only id and name, and drops email.
- Pick with "id" | "name" keeps exactly the listed keys.
- 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];
}
- A function that returns an array
- A generic function: the return type matches the element type of the array passed in
- A function that only accepts arrays of any
- 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
- T is a type parameter, which TypeScript fills in from the argument.
- Items is an array of T, and the function returns T or undefined.
- 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;
- ("red" | "green")[]
- string[]
- readonly ["red", "green"]
- ["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
- As const makes the array a readonly tuple.
- It keeps the exact literal values for each element, "red" and "green".
- 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;
}
}
- any; it allows any value
- unknown; it disables type checking
- never; if a new kind is added to Shape and not handled, this line becomes a compile error
- 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
- The switch covers the "circle" and "square" cases of Shape.
- In the default branch, TypeScript narrows shape to nothing, so its type is never.
- If a new kind is added to Shape and not handled, the never assignment fails to compile.
- 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 →