=>
Array.isArray(value)
const f = (
input: string | string[]
) => {
if (isArray(input)) {
// ✗ input: string | string[]
}
}
but
const g = (
input: string | string[]
) => {
if (Array.isArray(input)) {
// ✓ input: string[]
}
}
Because your isArray is just a regular boolean function, so in f, TS doesnt have any hint about the type of input. On the other hand, Array.isArray seems to be a type guard, so if it evaluates to true, TS knows input is of type string[]. If you want your isArray to be equivalent to Array.isArray, define it as a custom type guard: const isArray(value: any): value is Array => Array.isArray(value) (im not sure if thats the exact syntax)
Обсуждают сегодня