tezvyn:

How do you type and guard a string-or-string-array argument?

Source: typescriptlang.orgintermediate

Tests union typing and runtime narrowing. Annotate as `string | string[]`, then branch with `Array.isArray()`. Strong answers note `typeof` returns `"object"` for arrays and that assertions bypass safety.

Tests union types and TypeScript narrowing. A strong answer types the parameter as `string | string[]` and branches with `Array.isArray()`, handling arrays first and strings second. Candidates should explain that `typeof` returns `"object"` for arrays, so it cannot distinguish them, and that `Array.isArray()` is the idiomatic runtime check. They should avoid type assertions like `as string[]`, which bypass compiler safety.

Read the original → typescriptlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How do you type and guard a string-or-string-array argument? · Tezvyn