This TypeScript code implements similar functionality to the Go example. Here are some key points:
Instead of Go’s iterators, we use TypeScript’s generators, which provide similar functionality.
The List class is implemented with a push method and an all generator method that yields all elements.
The genFib function is a generator that produces Fibonacci numbers indefinitely.
In the main function, we demonstrate how to use these generators in for...of loops, which is similar to Go’s range loops over iterators.
TypeScript doesn’t have a direct equivalent to Go’s slices.Collect, but we can use Array.from() to achieve the same result.
The example maintains the structure and explanation of the original, adapted for TypeScript’s syntax and features.
This code demonstrates how to implement and use generator-based iterations in TypeScript, providing functionality similar to Go’s new iterator feature.