Issue
This is my code:
function isArray(value) {
return Array.isArray(value)
}
function someFunction(value: Array<any> | boolean) {
if (isArray(value)) {
console.log(value.length)
} else {
console.log(value)
}
}
And I got this error:
In my opinion, I think I've checkd type of value and make sure it is a array. But compiler of typescript doesn't recognize. How could I reslove this?
Solution
function isArray(value: any): value is any[] {
return Array.isArray(value)
}
You need to add a return type to isArray, making it a type predicate.
More info here: https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
Answered By - Anastasia

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.