Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rclmenezes opened this issue Feb 1, 2018 · 0 comments · Fixed by #30790
Closed

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

rclmenezes opened this issue Feb 1, 2018 · 0 comments · Fixed by #30790
Assignees
Labels
Bug A bug in TypeScript

Comments

@rclmenezes
Copy link

rclmenezes commented Feb 1, 2018

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 :)

@rclmenezes rclmenezes changed the title Suggestion: make generators return a new type of class that extends Iterator Suggestion: make throw and return required fields for Generators Feb 1, 2018
@rclmenezes rclmenezes changed the title Suggestion: make throw and return required fields for Generators Suggestion: make throw and return required fields for generators Feb 1, 2018
@mhegazy mhegazy added the Bug A bug in TypeScript label Feb 1, 2018
@mhegazy mhegazy added this to the TypeScript 2.8 milestone Feb 1, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, TypeScript 3.1 Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants