Advanced
Let only the list decide the type
Write firstOr<T>(xs: T[], fallback: ...): T that returns the first item, or fallback when the list is empty.
Only xs should decide T. If xs is a list of "a" | "b", then firstOr(xs, "c") must be a type error, and firstOr(["a"], "z") must still work. Use NoInfer.
What Check tests
- Your code has no type errors
- The program prints a and z
- A fallback of another type is rejected
- A fallback outside the list's literal type is rejected
- A fallback of the same type is accepted
- The result keeps the list type
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Press Run to see the page.
Console
Nothing printed yet.