Skip to content

yield operator in generator functions has any return type #26959

Closed
@freddi301

Description

@freddi301

Search Terms

  • generator
  • yield

Suggestion

  • Correct return type on yield operator
  • Correct return type on yield* operator
  • Correct type for generator function return

Use Cases

better type safety

redux-saga
koa.js

Example

now:

function* f() { // :IterableIterator<number>
  const x = yield 0; // x is any
  const y = yield* [2, 3, 4]; // y is any
}

desired:

function* f1(): TypedIterableIterator<number, number> {
  const x = yield 0; // x is number
  const y = yield* [2, 3, 4]; // y is number
  yield x + y;
}
function* f2() { // :IterableIterator<number, number>
  const x: number = yield 0;
  const y: number = yield* [2, 3, 4];
  yield x + y;
}
function* f3(): TypedIterableIterator<number> {
  const x = yield 0; // x is number
  const y = yield* [2, 3, 4]; // y is number
  yield x + y;
}

interface TypedIterableIterator<T, N = any> {
  next(value: N): T;
}

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. new expression-level syntax)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions