Skip to content

Generator functions may not return IteratorObject #63561

Description

@laverdet

🔎 Search Terms

If you have a generator function which a declared type of IteratorObject<T> and an implicit return, you receive the error "A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value. ts(2355)". I would expect us to be able to return these objects, since they adhere to the IteratorObject<T, unknown, unknown> interface.

This seems semi-related to the TypeScript 5.1 announcement easier implicit returns for undefined returning functions.

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about [Iterators and Generators]

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/PTAEBUAsEsGdTqA9gawFADMCuA7AxgC7RI6gBUGSSAFAJQBcoAkgQKYBOAhgUuwPIAjAFatCAHhxYAtgI4A+UAG80oUAE9orADYATUAEYA3CtDtWBLOxzGAvmjQhQAQVDZ8REqADukJLFagOqJanGZ6BGoADgGIOKzQBJAcoADkuEEY0HE6KQA0qQBuSNA5+Ti8qZw4aimgUliwBKbmlqScoAWcWlisAHQEsNQATADMAKxjtJi4hMSkZAKhdIwsHNy8giLikjLySiYa2npGaHYOYE5asEj5qNPuc+TmeMvMbFw8-MKiBBLSsuwFMpVD5oFoAtQCOwerR1JpdAZbEA

💻 Code

// This is ok
function *foo(): IteratorObject<number> {
  yield 1;
  return;
}

// A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.ts(2355)
function *bar(): IteratorObject<number> {
  yield 1;
}

// Also, ok
function *etc(): IteratorObject<number> {
  while (true) yield 1;
}

🙁 Actual behavior

"A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.ts(2355)"

🙂 Expected behavior

This should be allowed.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions