microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.23k stars 12.52k forks source link

Generator that never returns should have return type 'never' #33430

Open KSXGitHub opened 5 years ago

KSXGitHub commented 5 years ago

Search Terms

Suggestion

Generator function that yields T but never return should return Generator<T, never, unknown>

function * generator<T> (x: T) {
  while (true) yield x
}

const iter = generator(3) // expect: Generator<number, never, unknown>

Use Cases

Examples

Checklist

My suggestion meets these guidelines:

AnyhowStep commented 5 years ago

https://github.com/microsoft/TypeScript/issues/11296#issuecomment-267094790

https://github.com/microsoft/TypeScript/issues/16608

https://github.com/microsoft/TypeScript/pull/8767

RyanCavanaugh commented 5 years ago

Appreciate the links @AnyhowStep but I don't think the motivation for hiding never return types in function declarations necessarily applies to generator functions.

@rbuckton thoughts?