Intermediate
Reading by Index Safely
noUncheckedIndexedAccess is on, so items[items.length - 1] has the type number | undefined: the list might be empty. lastOf promises a number, so the checker rejects it.
Make lastOf return the last item, or 0 when the list is empty, without using ! or as. The console must print 3 and 0.
What Check tests
- Your code has no type errors
- The console prints
3and0 lastOf("abc")is rejected by TypeScript- No
!and noasassertion
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.