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

Type Iterator#next() argument (yield expression) #32998

Closed
5 tasks done
pauldraper opened this issue Aug 21, 2019 · 5 comments
Closed
5 tasks done

Type Iterator#next() argument (yield expression) #32998

pauldraper opened this issue Aug 21, 2019 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@pauldraper
Copy link

pauldraper commented Aug 21, 2019

Search Terms

Iterator.next() type, yield expression type

Suggestion

Type Iterator.next()/yield. I suggest Iterator<A, B>, where A is the input type, and B is the output type.

Mentioned in #10148 and #14883.

Required by #32283.

Alternative proposal in #32523, those this requires more drastic changes to TypeScript's type model.

Use Cases

Pretty self-explanatory; I want typed expressions. It's why I use TypeScript.

And currently, all yield ... expressions have no typing. (They are typed as any).

Examples

function* totalLength() {
  let result = 0;
  while (true) {
    result += (yield result).length;
}

const t = totalLength();
t.next('abc'); // 0
t.next('def'); // 3
t.return(''); // 6

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@pauldraper
Copy link
Author

pauldraper commented Aug 21, 2019

And actually, Iterator has additional problems, like return?(value?: any): IteratorResult<T> which is just simply wrong.

To be more concrete, the types should be

interface Iterator<T> {
    next(): IteratorResult<T>;
}

interface Generator<A, B> extends Iterator<B> {
    next(value?: A): IteratorResult<B>;
    return<T>(value?: T): IteratorResult<T>;
    throw(e?: any): IteratorResult<B>;
}

The optional argument is problematic for having non-undefined values inside the generator, but I don't have a better suggestion.

@AlCalzone
Copy link
Contributor

And currently, all yield ... expressions have no typing. (They are typed as any).

I think you should take a look at the TS 3.6 release notes
https://devblogs.microsoft.com/typescript/announcing-typescript-3-6-rc/

@RyanCavanaugh
Copy link
Member

See #30790 and its linked issues

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 21, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@pauldraper
Copy link
Author

pauldraper commented Aug 24, 2019

Excellent, thanks @AlCalzone for linking to the 3.6 RC notes.

@typescript-bot I think you might not be working correctly (you didn't actually close).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants