Deviopath › TypeScript

Narrowing with Array.isArray

Easy · Narrowing

Write countItems(value: string | string[]): number.

- If value is an array, return its length.

- If it is a string, it is a comma-separated list like "a,b,c": return the number of items. An empty string has 0 items.

Use Array.isArray(value) to narrow the union.

Next: Keep Only Strings