Skip to content

Suggestion: make throw and return required fields for generators #21527

Closed
@rclmenezes

Description

@rclmenezes

Currently, GeneratorFunctions return a Generator, which simply extends Iterator:

interface Generator extends Iterator<any> { }

This is unfortunate because next and throw are optional in Iterators:

interface Iterator<T> {
    next(value?: any): IteratorResult<T>;
    return?(value?: any): IteratorResult<T>;
    throw?(e?: any): IteratorResult<T>;
}

GeneratorsFunctions could theoretically throw an error at any time! In addition, they do always return something, even if it's undefined. Therefore, simply extending Iterator isn't specific enough.

So why not make throw and return required properties of Generator?

The current situation causes some rather frustrating problems. For example, this ticket.

True, the ECMA2015 schema says:

A Generator object is an instance of a generator function and conforms to both the Iterator and Iterable interfaces.

However, I'd argue that a more specific implementation of Iterator still conforms to Iterator :)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions