# TypeScript Quiz

## Question 1

What is the primary benefit of TypeScript over JavaScript?

A) Faster runtime performance
B) Catch type errors at compile time
C) Smaller bundle size
D) Built-in testing framework

<!-- ANSWER: B -->
<!-- EXPLANATION: TypeScript's main value is static type checking — it catches type mismatches, typos, and incorrect API usage before the code runs, reducing runtime bugs. -->

## Question 2

What does `interface User { name: string; email?: string }` mean for `email`?

A) email must be present
B) email is optional — may be omitted
C) email can be string or undefined
D) email has a default value

<!-- ANSWER: B -->
<!-- EXPLANATION: The ? makes the property optional. Objects of type User can omit email. If present, it must be a string. -->

## Question 3

How do you narrow a union type `string | number` to use string methods?

A) Use `as string` to cast
B) Use `typeof value === 'string'` in a conditional
C) Use `value.toString()` always
D) TypeScript automatically narrows

<!-- ANSWER: B -->
<!-- EXPLANATION: typeof value === 'string' is a type guard. In the true branch, TypeScript narrows value to string, allowing .toUpperCase() etc. Casting with 'as' bypasses checking. -->

## Question 4

What does `T extends keyof U` mean in a generic?

A) T must be a subtype of U
B) T must be one of the keys of U
C) U must have property T
D) T and U are the same type

<!-- ANSWER: B -->
<!-- EXPLANATION: keyof U produces a union of U's keys. extends here means T is constrained to be one of those keys, enabling safe property access. -->

## Question 5

What does `Partial<User>` do?

A) Makes User extend another type
B) Makes all properties of User optional
C) Makes User immutable
D) Picks the first property of User

<!-- ANSWER: B -->
<!-- EXPLANATION: Partial<T> is a utility type that makes every property of T optional. Useful for update payloads where you might only change some fields. -->

## Question 6

When would a JavaScript error occur that TypeScript would have caught?

A) Never — TS prevents all JS errors
B) When using `any` or type assertions to bypass checks
C) Only in Node.js
D) When the compiler is disabled

<!-- ANSWER: B -->
<!-- EXPLANATION: TypeScript can be bypassed with any, type assertions (as), or @ts-ignore. In those cases, invalid code can reach runtime. -->

## Question 7

<!-- VISUAL: fill-blank -->

Complete the type annotation for a function that returns a Promise of User:

```typescript
async function fetchUser(id: string): ___0___<User> {
  const res = await fetch(`/users/${id}`);
  return res.json();
}
```

<!-- ANSWER: Promise -->
<!-- EXPLANATION: Async functions return a Promise. The return type should be Promise<User> to reflect that the function returns a Promise that resolves to a User. -->

## Question 8

<!-- VISUAL: fill-blank -->

Complete the generic function that picks a property from an object:

```typescript
function getProp<T, K ___0___ keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}
```

<!-- ANSWER: extends -->
<!-- EXPLANATION: K extends keyof T constrains the generic K to be one of the keys of T. This ensures key is a valid property name and the return type T[K] is correctly inferred. -->
