microsoft / microsoft/TypeScript

recursive return type for deterministic generators

Open
#27,267 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

If a generator neither yields inside a loop nor defers to another generator, it's considered deterministic. For any given number of next calls, the compiler could theoretically reduce the generator to the exact type of its next yield. Of course, because generators are mutable, this isn't true. But what if I'm using a library that allows for immutable generators? For every next call, a new generator is returned. Maybe it should be possible to introspect the order of yields in a deterministic generator, and use that to deduce a recursive type structure that represents an immutable generator.

import immutagen from 'immutagen'

const gen = immutagen(function* foo() {
  yield 0
  yield 1
})

const a = gen()
const b = a.next()
typeof a // => { value: 0, next: () => typeof b }
typeof b // => { value: 1, next: undefined }

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by checking how TypeScript currently types generator functions and Generator.next results, then compare that behavior with the immutable-generator example. Define whether recursive yield-state types are feasible and what type-level behavior would count as done; the issue names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.