sindresorhus / is

Type check values
MIT License
1.68k stars 109 forks source link

is.iterable returns IterableIterator instead of Iterable #128

Closed xamgore closed 2 years ago

xamgore commented 3 years ago

Let's dive into the source code:

https://github.com/sindresorhus/is/blob/d528545e02de3396ea900cd93af478292f0697ee/source/index.ts#L184

Only [Symbol.iterator] property is checked, meaning the value is at least Iterable<T>. It may be IterableIterator<T> if the presence of one more property, next, is ensured.

interface Iterable<T> {
    [Symbol.iterator](): Iterator<T>;
}

interface IterableIterator<T> extends Iterator<T> {
    [Symbol.iterator](): IterableIterator<T>;
}

interface Iterator<T, TReturn = any, TNext = undefined> {
    next(...args: [] | [TNext]): ...;
    return?(value?: TReturn): ...;
    throw?(e?: any): ...;
}

Do you agree with it? Would you mind I make a PR?

sindresorhus commented 3 years ago

Sounds good. PR welcome. Shouldn't it check all the methods though, not just next?

And asyncIterable should be fixed too.

xamgore commented 3 years ago

return and throw are optional, so that's fine. Would you describe this change as breaking major or minor?

sindresorhus commented 3 years ago

I would describe it as a bug fix.

sindresorhus commented 3 years ago

@xamgore Still interested in doing a pull request? No worries if not. Just bumping in case you forgot :)

sdotson commented 2 years ago

Taking a crack at this here: https://github.com/sindresorhus/is/pull/149