Skip to content

Iterables and generators: arrays and strings as Iterable<T>, typed generator yields (TSLANG 153) - #973

Merged
ASDAlexander77 merged 5 commits into
mainfrom
iterable-generator
Nov 21, 2026
Merged

ASDAlexander77 merged 5 commits into
mainfrom
iterable-generator

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

This is the compiler side of the default library's iteration types (ASDAlexander77/TypeScriptCompilerDefaultLib#113). There, Iterable<T>, Generator<T, TReturn, TNext> and the rest are objects with next.

What changed

  • An array or a string can now be passed where an iterator interface is expected. For example, sum([1.5, 2]) where the parameter is v: Iterable<number>, or cat("abc") where it is v: Iterable<string>.
    • Before, this failed with "type 'number[]' can't be cast to interface 'Iterable': it has none of the interface's members".
    • Now the call gets a generator over the elements. Its helpers (__array_values<T> and __string_values<T>) are generated from source once per module, and their element type is the interface's. So an s32[] given to an Iterable<number> is converted first.
    • Under -mm=own the generator borrows the array.
  • A generator declared Generator<T> (or Iterator<T>, Iterable<T>, IterableIterator<T>) now yields T: its next() is declared { value: T; done: boolean }, so yield 1 gives a number.
    • Before, the yield type came from the literal (s32), which failed with "can't assign '() => {value:s32, ...}' to '() => {value:number, ...}'".
  • A generator declared Generator<T, TReturn> whose return gives a value of another type is now a compile error at the return. Before, return "end" in a Generator<number, string> was silently converted to a number and read as 0.

Verification

  • Full ctest: 8768 tests, 0 failures.
  • New tests: 00iterables.ts and 00generator_typed_yield.ts (compile, JIT and rc), and the diagnostic test generator_return_of_other_type.ts.
  • TypeScript compiler-tests sweep: 3244 → 3255 passing, with no crashes. The only regression is binaryArithmeticControlFlowGraphNotTooLarge timing out; it also timed out in the batch 23 baseline.

Still open (in TSLANG 153)

  • next(value) / TNext, return(), throw(): I tried the next(value?: TNext) parameter; it works when the generator is cast to an interface that declares that parameter. But next() is then also cast to interfaces that declare next: () => ... (a program's own, or the library's Iterable). An object's method with a different number of parameters has no adapter in an interface vtable; classes have one. So that adapter comes first.
  • [Symbol.iterator] on a generator: I tried it and backed it out. The built-in map/filter on iterators then resolved its this through an outer alias, and under own a method returning this is refused.
  • -mm=own: a generator typed as any interface other than the library's instantiated Iterator<T> fails at its for...of with "a value takes a second reference". This was already the case before this change, and the same applies to an array passed as an Iterable<T>.

🤖 Generated with Claude Code

ASDAlexander77 and others added 5 commits November 21, 2026 12:49
…Generator<T>: next) is wanted: a generator over its elements, made from source once a module

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… yields T: next() is declared { value: T; done: boolean }; a returned value of another TReturn is an error, not a silent cast

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…under -mm=own, not async)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…enerator as its own iterator, the TReturn error

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…resolved its this through an outer alias); TSLANG 153

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit cc7df57 into main Nov 21, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the iterable-generator branch November 21, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant