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

Add guide on working with Effection and TypeScript #579

Closed
cowboyd opened this issue Oct 13, 2021 · 5 comments
Closed

Add guide on working with Effection and TypeScript #579

cowboyd opened this issue Oct 13, 2021 · 5 comments
Assignees

Comments

@cowboyd
Copy link
Member

cowboyd commented Oct 13, 2021

One of the gaps in Effection is that it has some idiosyncrasies in working with TypeScript. In particular with the way TS handles generators, there is no way for the right hand side of a yield expression to influence the inferred type of the left hand side. As a result, you need to explicitly annotate your intermediate values, otherwise TS infers them as being of type any

// `bare` is typed as `any`
let bare = yield Promise.resolve(10);

// to get type support you need to explicitly annotate
let num: number = yield Promise.resolve(10);

This can be problematic since you can actually get it wrong and get the compiler out of sync with the actual code which can result in mystifying runtime errors.

// this will type-check just fine
let num: number = yield Promise.resolve('not a number!');

The fact that there is not inference is confusing as seen here and so we should arm folks with the knowledge a) not to be suprised by it and b) how to work with it, because it's something that will likely happen very soon in their journey.

@jorgelainfiesta
Copy link
Contributor

I see. What would be required for TS to be able to infer the type correctly from the yield statement?

@cowboyd
Copy link
Member Author

cowboyd commented Oct 13, 2021

It would require a new release of TypeScript, but given the response from one of the folks on the core team, they are still not sure how to approach it yet microsoft/TypeScript#38534 (comment)

@cowboyd
Copy link
Member Author

cowboyd commented Oct 13, 2021

See also microsoft/TypeScript#36967

I also have a prototype of Effection that works with TypeScript out of the box: /~https://github.com/cowboyd/nano but it is only a very recent discovery, and would imply some pretty major breaking changes, and so we reasonably wouldn't be able to leverage it for at least a year.

@jorgelainfiesta
Copy link
Contributor

Gotcha thanks, I was asking to figure out the way of explaining the limitation in the guides.

@cowboyd
Copy link
Member Author

cowboyd commented Jan 7, 2022

Closed by #599

@cowboyd cowboyd closed this as completed Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants