TypeScript interview questions

17 questions from the TypeScript Fundamentals course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 2: Basic Types

1. What happens if you assign a `string` to a variable typed `number`?

  1. It works, JS is flexible at runtime
  2. TypeScript throws a compile-time error
  3. The value silently converts to NaN
  4. It is ignored by the compiler
Show answer

Answer: TypeScript throws a compile-time error

The TS compiler checks assignments against declared types and refuses to build mismatched code.

Lesson: Primitive Types

Module 3: Functions

2. Which makes a function parameter optional?

  1. A trailing `?` after its name
  2. Wrapping it in `unknown`
  3. Putting it first in the parameter list
  4. Declaring it with `void`
Show answer

Answer: A trailing `?` after its name

`name?: string` tells TS the caller may omit this argument.

Lesson: Typed Function Parameters

3. What is the return type of `async function f(): Promise<number>`?

  1. number
  2. Promise<number>
  3. void
  4. unknown
Show answer

Answer: Promise<number>

An async function always wraps its result in a Promise.

Lesson: Async Functions & Promise Types

Module 4: Objects & Interfaces

4. Which of these can name a union type like `string | number`?

  1. interface only
  2. type only
  3. Neither
  4. `enum` only
Show answer

Answer: type only

Interfaces can only describe object shapes; unions need a `type` alias.

Lesson: Type Aliases vs Interfaces

Module 6: Enums

5. In `enum Status { Pending, Active, Closed }`, what is `Status.Closed`?

  1. 0
  2. 1
  3. 2
  4. 3
Show answer

Answer: 2

Members default to 0, 1, 2 in declaration order, so Closed is 2.

Lesson: Numeric Enums

Module 7: Classes

6. Which modifier allows access from a subclass but not from outside code?

  1. public
  2. private
  3. protected
  4. readonly
Show answer

Answer: protected

protected members are visible in the declaring class and its subclasses, but not to outside code.

Lesson: Access Modifiers & readonly

Module 9: Type Narrowing

7. Inside `if (typeof id === "string") { ... }`, what type does TS assign to `id`?

  1. string | number, unchanged
  2. string
  3. any
  4. never
Show answer

Answer: string

The typeof check narrows the union to just string inside that block.

Lesson: Type Guards

Module 11: Modules

8. How many `export default` statements can one file have?

  1. At most one
  2. Unlimited
  3. Exactly one per interface
  4. Two, one for types and one for values
Show answer

Answer: At most one

A module can have many named exports but only a single default export.

Lesson: Modules: import & export

Module 12: Advanced Basics

9. What does `"strict": true` in tsconfig.json do?

  1. Enables a single extra check
  2. Bundles several strict type-checking options together
  3. Only affects the output file names
  4. Disables type checking in test files
Show answer

Answer: Bundles several strict type-checking options together

strict is a shorthand that turns on strictNullChecks, noImplicitAny, and several other checks at once.

Lesson: Strict Mode & Compiler Errors

10. Which `tsconfig.json` option enables the full set of strict checks?

  1. noEmit
  2. strict
  3. target
  4. outDir
Show answer

Answer: strict

`strict: true` bundles strictNullChecks, noImplicitAny and more.

Lesson: Strict Mode & Compiler Errors

11. Which type should you prefer over `any` for unpredictable data?

  1. unknown
  2. object
  3. Function
  4. never
Show answer

Answer: unknown

`unknown` forces you to narrow before use.

Lesson: Strict Mode & Compiler Errors

12. What does a tuple like `[string, number]` enforce?

  1. Any length of mixed values
  2. A fixed length with a type at each position
  3. Only string values
  4. Readonly access
Show answer

Answer: A fixed length with a type at each position

Tuples fix both length and per-position types.

Lesson: Strict Mode & Compiler Errors

13. How do you let TypeScript narrow a union by a shared field?

  1. Use a discriminated union with a literal `kind`
  2. Cast with `as any`
  3. Use `Partial<T>`
  4. Declare the field optional
Show answer

Answer: Use a discriminated union with a literal `kind`

A literal discriminant lets `switch` narrow each variant.

Lesson: Strict Mode & Compiler Errors

14. What does `Partial<User>` produce?

  1. A type with every property optional
  2. A type with every property readonly
  3. A type with only required keys
  4. A union of keys
Show answer

Answer: A type with every property optional

`Partial<T>` makes all properties optional.

Lesson: Strict Mode & Compiler Errors

15. What does `T extends { length: number }` do?

  1. Constrains T to types that have a length property
  2. Makes T a number
  3. Adds a length field at runtime
  4. Disables inference
Show answer

Answer: Constrains T to types that have a length property

Constraints narrow what a generic accepts while staying reusable.

Lesson: Strict Mode & Compiler Errors

16. What does `import type { Point }` do to the output JavaScript?

  1. Adds a runtime import
  2. Is erased at compile time
  3. Bundles the type
  4. Creates a class
Show answer

Answer: Is erased at compile time

Type-only imports are removed from the emitted JS.

Lesson: Strict Mode & Compiler Errors

17. What type does `await` give you for `Promise<User>`?

  1. Promise<User>
  2. User
  3. unknown
  4. void
Show answer

Answer: User

`await` unwraps the Promise to its resolved type.

Lesson: Strict Mode & Compiler Errors

← Back to the course