fix(fs): expose iterator helpers on walk/walkSync and expandGlob/expandGlobSync#7136
Open
fibibot wants to merge 2 commits into
Open
fix(fs): expose iterator helpers on walk/walkSync and expandGlob/expandGlobSync#7136fibibot wants to merge 2 commits into
walk/walkSync and expandGlob/expandGlobSync#7136fibibot wants to merge 2 commits into
Conversation
…`/`expandGlobSync`
The four generator functions in `@std/fs` were annotated with the
`IterableIterator<WalkEntry>` / `AsyncIterableIterator<WalkEntry>`
return types, which widen the actual `Generator` / `AsyncGenerator`
runtime type and hide the ES2025 iterator helpers (`.map`, `.filter`,
`.take`, etc.) at the type level.
Switch the annotations to `Generator<WalkEntry>` /
`AsyncGenerator<WalkEntry>` so callers can chain helpers without
widening, e.g. `walkSync(".").map((v) => v.name)`.
Closes #7099
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7136 +/- ##
=======================================
Coverage 94.61% 94.61%
=======================================
Files 634 634
Lines 51822 51826 +4
Branches 9336 9348 +12
=======================================
+ Hits 49029 49035 +6
+ Misses 2218 2216 -2
Partials 575 575 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
The previous commit added `Generator<WalkEntry>` annotations and tests that exercise `.map(...).toArray()`. Both regressed on Deno v1.x (TypeScript 5.5, which predates iterator helpers in `lib.es2025.iterator.d.ts`): - `expandGlob` / `expandGlobSync` ended with `yield* currentMatches`, where `currentMatches` is `WalkEntry[]`. Array's iterator declares `TNext` as `undefined`, but the outer `Generator<WalkEntry>`'s `TNext` defaults to `unknown`, so TS 5.5 reports TS2766. Newer TS widens the array iterator to `unknown` and the call type-checks. Replace the delegation with an explicit `for ... yield` loop so it compiles on both. - The new tests called `.map(...).toArray()` on the returned iterator, which TS 5.5 rejects (no iterator helpers in lib). Switch to a type-level assertion: `const iter: Generator<WalkEntry> = walkSync(...)`. This still enforces the regression — `Generator<T>` requires the `.return()`/`.throw()` methods that `IterableIterator<T>` leaves optional, so the assignment fails if the return type widens back — but compiles on every TS version where `Generator<T>` is defined.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
walk,walkSync,expandGlob, andexpandGlobSyncare generator functions, but their return-type annotations widened the type to(Async)IterableIterator<WalkEntry>, hiding the ES2025 iterator helpers (.map,.filter,.take, …) at the type level. The runtime objects do have those helpers (on the sync side), so the original reprowas a type-only bug.
This PR changes the annotations to
Generator<WalkEntry>/AsyncGenerator<WalkEntry>for all four functions so callers can chainhelpers without widening, and the async pair is forward-compatible for
when async iterator helpers ship in the standard library.
Note:
walkandwalkSyncneed an explicit return type (TS7023 — theyreference themselves recursively), so the annotations are tightened
rather than dropped entirely.
Test plan
Regression tests are co-located in
fs/walk_test.tsandfs/expand_glob_test.ts. The sync tests exercise.map(...).toArray()at both type-check and runtime; the async tests assert that
AsyncGeneratorshape is preserved (no async iterator helpers exist atruntime yet, so they just exercise
.next()/.return()).deno fmt --checkdeno lintdeno run -A _tools/check_docs.tswalkSync()/expandGlobSync() returns a generator that exposes iterator helpers,walk()/expandGlob() returns an async generatorwalkSyncreturns a type without iterator helpers #7099 type-checksCloses #7099
Closes bartlomieju/orchid-inbox#62