EduFlow
Advanced TypeScript Patterns

Checkpoint: Generics in Depth

Five questions on this section. Answer all of them, then see your score and why each answer is right or wrong.

Question 1 of 5

Which signature lets firstWhere(orders, (o) => o.status === 'shipped') return Order | undefined, and firstWhere(products, …) return Product | undefined, with o typed in the callback?

Question 2 of 5

What is the type of t.value?

TypeScript
function tag<T>(value: T) {
  return { value };
}
const t = tag('shipped');
Question 3 of 5

setDefault(['standard', 'express'], 'overnight') should be rejected because 'overnight' is not one of the options. Which signature does that?

Question 4 of 5

Callers of Cartwheel’s client write api.get<Order>('/orders/1042') everywhere, and last week someone wrote api.get<Order[]>('/orders/1042'). What is the best fix?

Question 5 of 5

function useQuery<TData, TKey extends string>(key: TKey, fetcher: () => Promise<TData>) is called as useQuery<Order>('order-1042', loadOrder) and fails with “Expected 2 type arguments, but got 1”. What is the best fix at the call site?

5 questions unanswered