-
Notifications
You must be signed in to change notification settings - Fork 710
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
[C3] Use satisfies ExportedHandler
for handlers in templates
#5607
Conversation
🦋 Changeset detectedLatest commit: c451235 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9472778657/npm-package-wrangler-5607 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/5607/npm-package-wrangler-5607 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9472778657/npm-package-wrangler-5607 dev path/to/script.js Additional artifacts:npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9472778657/npm-package-create-cloudflare-5607 --no-auto-update npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9472778657/npm-package-cloudflare-kv-asset-handler-5607 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9472778657/npm-package-miniflare-5607 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9472778657/npm-package-cloudflare-pages-shared-5607 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9472778657/npm-package-cloudflare-vitest-pool-workers-5607 Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice improvement! I really like it 😄
Thanks a bunch @morinokami ❤️
Could you please create a changeset as well for this change? (I'd classify this as a feature
I guess 🤔)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@morinokami thanks for the changeset 😄
Again LGTM 😃
I think this actually results in worsened DX sadly. Take the following Worker for example: export interface Env {}
export default {
async fetch(request, env, ctx) {
return true;
},
} satisfies ExportedHandler<Env>; This errors with:
Which is a pretty complex error that requires reading through, and it ends up highlighting the vs if you use the original setup: export interface Env {}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
return true;
},
}; The error you get here is so much clearer:
And it highlights the actual place in the code where the issue is: This is obviously a contrived example, but for more complex workers, with hundreds of line, isolating errors is now significantly more complex, especially for those newer to TypeScript. Here's a more detailed example: cloudflare/cloudflare-docs#14006 |
@Cherry thanks a bunch for the comment 🙂 I can see your point, let's see what the rest of the team thinks 👍 Especially I'd be keen to see the response to cloudflare/cloudflare-docs#14007 and follow whatever the consensus there is (move await from or stick with |
@Cherry Thank you for your comment! Indeed, particularly for newcomers to TypeScript, it might be easier to understand errors if the return statement line is highlighted directly, while the However, it is also true that specifying the type offers various advantages. Here are some benefits worth considering:
Due to these advantages of specifying types, some developers, including myself, prefer to use constructs like I believe it's important to consider all of these factors collectively. Again, thank you for your valuable feedback 👍 |
Thank you for the well thought out response @morinokami!
While I can see the benefit here, this isn't always true.
This is definitely nice, but I would argue is not the most common use-case. If you're working with a brand new product like Queues, or Email Workers. it's pretty much a guarantee you are going to have to check the docs for what to add to your
This is valid, but simply is not going to happen with Workers due to their compatibility guarantees. If this did ever happen, it would be behind a compatibility date as per the backwards compat model in workers which would result in there being multiple different
I strongly believe this is a power user mentality, which is valid, but not what I'd consider C3 to be targeting with their templates. In conclusion, while some of the points you have made are valid in isolation, I do not believe they trump the serious developer experience issues that are present with handling errors, a much more common scenario, for new users to the platform. |
@Cherry Even if the However, I do acknowledge that there are several valid points in your perspective. I think this issue doesn't have a clear right or wrong answer; rather, the right approach may vary based on one's experience and perspective. My hope is that the final decision delivers the greatest benefit to developers. The maintainers are likely more familiar with what Cloudflare users need, and as @dario-piotrowicz suggests, I believe that they should be the ones to make the final decision. I appreciate this meaningful discussion, and thank you again! |
Even if/once automated, I'm not aware of any way with this typing approach to get meaningful and useful errors for line numbers within the
I absolutely agree here. My personal opinion is that C3 targets newer users (to both Cloudflare, and maybe even JS/TS) way more than power users, and optimising for them should be the priority. Helpful and meaningful errors will be a huge thing for these folks. For advanced use-cases, or when you're intimately familiar with Workers and your I too appreciate the meaningful discussion, and look forward to what teams at Cloudflare decide is the best approach here. |
@morinokami Thanks again for the PR 🙂 Although I can see the benefit in your approach after better understanding the various tradeoffs I'm also leaning towards @Cherry's version Although less robust, it is likely much more friendly for newcomers, more advanced users should be able to switch easily to the |
@Cherry will not like me for this, and I know his opinion about the explicit type cast syntax and See my screenshot attached to see what gets underlined. In case 3 (where both are annotated) get the DX of the return false line getting underlined and the error shown is the same as case 2, and we get the future-proof syntax should a user wish to add queue/tail/email/... to their worker. |
@dario-piotrowicz Thank you for your comment!
Yeah, I think it would be problematic for those who want to use this type if it were to be completely removed from the Docs code examples. Currently, it seems that there is very little information about the type in the Docs (as shown in the screenshot below, there is only one search result, and it does not even describe the |
@DaniFoldi Thanks for your feedback! I really like your idea. Indeed, it seems that |
With something like the following, the errors are indeed handled in a way that's helpful for new users: export interface Env {}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const something = false;
const somethingElse = true;
if(something){
return 'nope';
}
if(!somethingElse){
return 'nope';
}
return new Response('Hello, world!')
},
} satisfies ExportedHandler<Env>; The only drawbacks to this approach are the duplicate nature now in userland typings, and ensuring you match up
If this is the approach folks want to take, I'd be okay with it, and it seems like a reasonable compromise. It makes error output mostly useful (outside of mismatched typings), and helps with the previous concerns around autocomplete for additional handlers like |
See cloudflare/cloudflare-docs#14007 (comment), but TLDR I think we should use |
@penalosa Thank you! I've updated the code to match the agreed-upon style in cloudflare/cloudflare-docs#14007. @Cherry did the same thing there, so the docs and c3 are now in sync 😁 |
This looks great and would be awesome to land soon so future projects scaffolded with C3 get the best experience. |
I have run the C3 tests locally and they pass so I'm going to merge this PR. Thanks for the work and the discussion everyone! |
Looks like @workers-devprod is trying to award a holobyte, but something went wrong while doing so. See this page for more information. |
What this PR solves / how to test
Use
satisfies ExportedHandler
for handlers in templates. This will improve autocompletion in the editor and make the code cleaner.Author has addressed the following