ARTICLE AD BOX
In the following TypeScript custom type test function, I'm clearly testing that x[Symbol.iterator] is a function before calling it, yet TypeScript type checking fails:
const isIterableIterator = <T,>(x: unknown): x is IterableIterator<T> => { return ( (x !== null) && (x !== undefined) && (typeof x === 'object') && ('next' in x) && (typeof x.next === 'function') && (Symbol.iterator in x) && (typeof x[Symbol.iterator] === 'function') && (x[Symbol.iterator].call(x) === x) ) }The error message I get is:
TS2571 [ERROR]: Object is of type 'unknown'. && (x[Symbol.iterator].call(x) === x) ~~~~~~~~~~~~~~~~~~If I change the parameter x: unknown to x: any, I don't get an error. But I'm trying to avoid using type any anywhere in my code.
Is there a way to get this to work without having to make my code harder to understand by creating a separate variable to hold the return value from calling x[Symbol.iterator]?
