Skip to content

fix(fs): return Generator from walkSync and expandGlobSync so iterator helpers are visible#7166

Open
LeSingh1 wants to merge 2 commits into
denoland:mainfrom
LeSingh1:fs-walk-generator-return-type
Open

fix(fs): return Generator from walkSync and expandGlobSync so iterator helpers are visible#7166
LeSingh1 wants to merge 2 commits into
denoland:mainfrom
LeSingh1:fs-walk-generator-return-type

Conversation

@LeSingh1
Copy link
Copy Markdown

Fixes #7099.

walkSync and expandGlobSync are generator functions, so at runtime they already have iterator helpers (map, filter, take, drop, toArray, ...). Their declared return type IterableIterator<WalkEntry> hides those helpers from TypeScript and forces callers to wrap with Array.from(...) or write a manual loop just to use them.

From the issue:

const a = walkSync(".");
const b = a.map(v => v.name);
// Property 'map' does not exist on type 'IterableIterator<WalkEntry>'.

Narrowing the return type to Generator<WalkEntry> exposes the helpers. Generator extends IterableIterator so existing callers stay assignment-compatible:

walkSync(".").map((e) => e.name).toArray(); // now type-checks
expandGlobSync("*").map((e) => e.name).toArray(); // ditto

The async siblings (walk, expandGlob) are intentionally left as AsyncIterableIterator<WalkEntry>. The async iterator helpers aren't in the TypeScript lib that Deno currently ships, so claiming them on the return type would mislead callers into writing code that breaks at type-check time. Happy to change this if the right lib is on by default and I missed it.

Verification: ran fs/walk_test.ts + fs/expand_glob_test.ts with -A, 63 pass / 4 fail — the 4 failures (accepts skip option as regExps ×2, walks fifo files on unix ×2) reproduce on clean origin/main so they're pre-existing and unrelated.

walkSync and expandGlobSync are generator functions, so at runtime
they already have iterator helpers (map, filter, take, drop, toArray,
...). Their declared return type IterableIterator<WalkEntry> hides
those helpers from TypeScript, forcing callers to wrap with
Array.from() or write a manual loop just to use them.

Narrow the return type to Generator<WalkEntry>. Generator extends
IterableIterator so existing callers stay assignment-compatible, but
the helpers become visible:

  walkSync(".").map((e) => e.name).toArray()  // now type-checks

Async versions (walk, expandGlob) keep AsyncIterableIterator because
the async iterator helpers aren't in the TypeScript lib that Deno
currently ships, so claiming them would mislead callers.

Fixes denoland#7099
@github-actions github-actions Bot added the fs label May 29, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.57%. Comparing base (cdf74a8) to head (a6756d0).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7166   +/-   ##
=======================================
  Coverage   94.57%   94.57%           
=======================================
  Files         636      636           
  Lines       52142    52142           
  Branches     9401     9401           
=======================================
  Hits        49315    49315           
  Misses       2249     2249           
  Partials      578      578           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Deno v1.x's TypeScript lib infers Generator<T>'s default TNext as
unknown, which conflicts with the inner walkSync's yield* delegation
expecting undefined (TS2766). Pin TReturn=void and TNext=undefined to
match what the generators actually produce.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs: walkSync returns a type without iterator helpers

1 participant