Deviopath › TypeScript

Last Item or Fallback

Easy · Generics

Write a generic function lastOr<T>(items: T[], fallback: T): T that returns the last item of the array, or fallback if the array is empty.

The type parameter T makes sure the fallback has the same type as the items.

Next: Wrap in an Array