`consume` types and `AnyIterable`
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- typescript
- Domain
- api
Research direction
Start at the consume overload declarations and implementation shown in the issue, then reproduce the TypeScript errors using the AnyIterable examples. Check how the overloads handle both Iterable and AsyncIterable inputs and verify that calls with either source type resolve to an appropriate return type.
Written by the indexing model from the issue text.
Description
It doesn't look like I can use the AnyIterable type with consume, even though it supports both Iterable<T> and AsyncIterable<T> as input:
import { consume } from 'streaming-iterables'
import type { AnyIterable } from 'streaming-iterables'
// specify return type otherwise typescript works out this is really `number[]`
function createSyncSource (): AnyIterable<number> {
return [1, 2, 3]
}
// specify return type otherwise typescript works out this is really `AsyncGenerator<number, void, undefined>`
async function * createAsyncSource (): AnyIterable<number> {
yield * [1, 2, 3]
}
const sourceArr = createSyncSource()
const sourceGen = createAsyncSource()
// works if I cast to the underlying type
consume(sourceArr as number[])
consume(sourceGen as AsyncIterable<number>)
// does not select the correct overload based on the possible types of input
consume(sourceArr)
consume(sourceGen)
The error is:
error TS2769: No overload matches this call.
Overload 1 of 2, '(iterable: Iterable<number>): void', gave the following error.
Argument of type 'AsyncIterable<number>' is not assignable to parameter of type 'Iterable<number>'.
Property '[Symbol.iterator]' is missing in type 'AsyncIterable<number>' but required in type 'Iterable<number>'.
Overload 2 of 2, '(iterable: AsyncIterable<number>): Promise<void>', gave the following error.
Argument of type 'AsyncIterable<number>' is not assignable to parameter of type 'AsyncIterable<number>'.
Property '[Symbol.asyncIterator]' is missing in type 'Iterable<number>' but required in type 'AsyncIterable<number>'.
If I add an additional overload to the type definition that's the same as the actual implementation it starts to work:
export function consume<T>(iterable: Iterable<T>): void
export function consume<T>(iterable: AsyncIterable<T>): Promise<void>
export function consume<T>(iterable: AnyIterable<T>): Promise<void> | void // <-- new overload
export function consume<T>(iterable: AnyIterable<T>) {
if (iterable[Symbol.asyncIterator]) {
return _consume(iterable)
}
for (const val of iterable as Iterable<T>) {
// do nothing
}
}
But then the return type is Promise<void> | <void>. I tried doing something clever like:
export declare type UnwrapToVoidOrVoidPromise<M extends AnyIterable<any>> = M extends Iterable<any> ? void : M extends AsyncIterable<any> ? Promise<void> : never;
but it's still Promise<void> | <void>. Maybe that's ok, I'm not sure.
- Dominant language
- TypeScript
- Stars
- 87
- Forks
- 6
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from reconbot/streaming-iterables
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
reconbot/streaming-iterables#396 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
reconbot/streaming-iterables#294 ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
reconbot/streaming-iterables#285 · 7 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
reconbot/streaming-iterables#284 ·
-
Dependency Dashboard Open
Difficulty 3/5 1-2 days Newbie friendliness 15/100
reconbot/streaming-iterables#280 ·
All issues in reconbot/streaming-iterables
Similar issues
-
comp/dashboard P3 type/bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
NousResearch/hermes-agent#117722 ·
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster no-stale P3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 76/100
-
code-quality refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·