TypeScript MCQs - Types, Interfaces, Unions and Type Narrowing (Set 1)

TypeScript MCQs - Types, Interfaces, Unions and Type Narrowing (Set 1) - CodingHumans

Almost every React and Node.js job now expects TypeScript, and interviewers want to see if you understand the type system, not just the syntax. This set covers the basics that come up most: any versus unknown, interfaces versus type aliases, union and literal types, and how TypeScript narrows types as your code runs through if and switch.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.

TypeScript MCQ series: Set 1 | Set 2 | Set 3 | Interview Preparation Hub

Topics covered in this set

  • Basic and special types: any, unknown, never, void
  • Interfaces vs type aliases, declaration merging
  • Union, intersection and literal types
  • Type guards: typeof, instanceof, in and custom predicates
  • Discriminated unions and exhaustiveness checks
  • Optional properties, readonly and strict null checks

TypeScript MCQs with answers and explanations

Q1. What is the main difference between any and unknown?

a) unknown must be narrowed before you can use it, while any disables type checking

b) They are identical aliases

c) any can only hold primitives

d) unknown can only be used for function return types

Q2. Which type represents a value that never occurs, such as the return type of a function that always throws?

a) never

b) void

c) null

d) undefined

Q3. What will TypeScript report for this code?

let id: string | number = 42;
id.toUpperCase();

a) No error, because unions allow any method

b) No error, because id is a string at runtime

c) Error: Property toUpperCase does not exist on type number

d) Error: id cannot be reassigned

Q4. Which feature is available for interfaces but not for type aliases?

a) Describing object shapes

b) Extending other types

c) Declaration merging (two declarations with the same name are combined)

d) Optional properties

Q5. Which of these can a type alias express that an interface cannot?

a) An index signature

b) An object with methods

c) A readonly property

d) A union such as type Status = "idle" | "loading" | "done"

Q6. What is the type of x?

const x = "hello";

a) String

b) "hello" (a string literal type)

c) any

d) string

Q7. Which operator narrows a union of object types by checking whether a property exists?

a) in

b) instanceof

c) keyof

d) typeof

Q8. What is a user-defined type guard?

a) A generic constraint using extends

b) A function whose return type is written as value is SomeType

c) A tsconfig option

d) A class decorator that validates types at runtime

Q9. In a discriminated union, what makes narrowing work?

a) Every member is declared with interface

b) Every member is a class

c) Every member has the same methods

d) Every member has a common property with a different literal type, such as kind: "circle"

Q10. How do you make the compiler error when a new member is added to a union but not handled in a switch?

a) Add a default case that returns null

b) Enable the noImplicitReturns option only

c) Use any in the default case

d) Assign the value to a variable of type never in the default case

Q11. With strictNullChecks enabled, which assignment is an error?

a) let b: string = null;

b) let a: string | null = null;

c) let d: unknown = null;

d) let c: string | undefined;

Q12. What does the non-null assertion operator ! in user!.name do?

a) Makes the property optional

b) Converts the value to a boolean

c) Checks for null at runtime and throws

d) Tells the compiler to treat the value as not null or undefined, without any runtime check

Q13. What is the type of A & B when A = { id: number } and B = { name: string }?

a) An object that has both id and name

b) never

c) An object that has either id or name

d) A union of number and string

Q14. What does readonly on a property guarantee?

a) The value is frozen at runtime with Object.freeze

b) The property is private

c) The property cannot be reassigned through that type at compile time

d) The property is optional

Q15. What does as const do in const roles = ["admin", "user"] as const;?

a) Infers a readonly tuple of literal types: readonly ["admin", "user"]

b) Makes the variable mutable

c) Has no effect on arrays

d) Converts the array to a Set

Quick revision notes

  • For values that come from outside your code, use unknown instead of any, then narrow.
  • Interfaces merge and type aliases do not. Type aliases can describe unions and tuples.
  • const infers literal types, let widens them.
  • Use discriminated unions with a never check to handle state safely.
  • readonly, ! and as are compile-time only and do nothing at runtime.

Frequently asked questions

Should I use interface or type in React props?

Either works. Many teams use type for props because it also handles unions, and interface when they want declaration merging or to extend class-like shapes. Sticking to one style in a codebase matters more than which one you pick.

Is TypeScript checked at runtime?

No. Types are removed when TypeScript compiles to JavaScript. For data from APIs or forms, check it at runtime with a schema library, and get the type from that schema.

What is the difference between never and void?

void means a function returns nothing useful (it may return undefined). never means it never returns at all, for example because it always throws.

Comments

Answered 0/0Correct 0