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

Yield Overrides #47548

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Yield Overrides #47548

wants to merge 3 commits into from

Conversation

arcanis
Copy link

@arcanis arcanis commented Jan 21, 2022

This PR implements #43632 and provides a way to annotate values in such a way that, when yielded, they return a specific other type. This allows libraries built around generators to provide proper typings:

declare function select<T>(fn: () => T): YieldType<T>;

function* generator() {
  const squareValue = yield select(state => state.number ** 2);
  assertEqual<number>()(squareValue);

  const isGreaterThan10 = yield select(state => state.number > 10);
  assertEqual<boolean>()(isGreaterThan10);

  const hexValue = yield select(state => state.number.toString(16));
  assertEqual<string>()(hexValue;
}

Fixes #43632

cc @andrewbranch for the playground link

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jan 21, 2022
@andrewbranch
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 21, 2022

Heya @andrewbranch, I've started to run the tarball bundle task on this PR at 9fc3cc5. You can monitor the build here.

@arcanis
Copy link
Author

arcanis commented Jan 21, 2022

Sorry about that, tests should be fixed; can you try to re-run the pack?

@andrewbranch
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 24, 2022

Heya @andrewbranch, I've started to run the tarball bundle task on this PR at c9f8bb2. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 24, 2022

Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/118156/artifacts?artifactName=tgz&fileId=49D5863517A089D4E367A13C0EC34BE731BA8B8BB714FF38756C82B1AA70226002&fileName=/typescript-4.6.0-insiders.20220124.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

/**
* Marker for yield expressions return types
*/
interface YieldType<T> { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use type YieldType<T> = intrinsic;? (see #46836 for ThisType above)

@neurosnap
Copy link

I'd love to help in anyway possible to get this across the finish line. Is there anything I can do to help?

@arcanis
Copy link
Author

arcanis commented Feb 27, 2022

It should already be ok apart from a couple of extra tests; next blocker is to find someone to champion it in the TS team.

@neurosnap
Copy link

How can we find someone to champion this PR?

@andrewbranch
Copy link
Member

To be clear, nobody on the TypeScript team ever indicated that this approach to solving this problem would be mergeable. @arcanis wrote it as an experiment purely to help the discussion in #43632 along. Given the discussion there, I personally feel that this is not the right approach—this is essentially just a type assertion, but piggy-backing on non-type-assertion syntax in such a way that nobody will recognize that it’s just as unsafe. The next step is not finding someone to champion this PR; we are still on the very first step: exploring possible solutions that could be driven to consensus.

@scorbiclife scorbiclife mentioned this pull request Sep 9, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

💡 Yield Overrides
6 participants