KEMBAR78
feat(type-generation): improve type generation for workflow by hiendv · Pull Request #9705 · cloudflare/workers-sdk · GitHub
Skip to content

Conversation

hiendv
Copy link
Contributor

@hiendv hiendv commented Jun 23, 2025

Fixes #9436

Make Workflow type generation stricter with generateImportSpecifier (thanks to @ottomated #6259).

Before:

interface Env {
	MY_WORKFLOW: Workflow;
}

After:

interface Env {
	MY_WORKFLOW: Workflow<Parameters<import("./src/index").MyWorkflow['run']>[0]['payload']>;
}

This type-hint looks a bit messy because params is only reflected with the run function. I think we could improve that by introducing a utility type (for now, I keep it simple with the "verbose" version).

type AnyWorkflowClass = new (...args: any) => WorkflowEntrypoint<any, any>;

export type GetWorkflowPayload<T extends AnyWorkflowClass> =
  InstanceType<T> extends WorkflowEntrypoint<any, infer P> ? P : never;

And then:

interface Env {
	  MY_WORKFLOW: Workflow<GetWorkflowPayload<typeof MyWorkflow>>;
}

  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because:
  • Wrangler / Vite E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • [] I don't know
    • Required
    • Not required because: It's not a breaking change and all we did is adding a type to unknown generic
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: It doesn't directly serve users
  • Wrangler V3 Backport
    • TODO (before merge)
    • Wrangler PR:
    • Not necessary because: not a patch change

@hiendv hiendv requested a review from a team as a code owner June 23, 2025 08:59
@changeset-bot
Copy link

changeset-bot bot commented Jun 23, 2025

🦋 Changeset detected

Latest commit: 615ca52

The changes in this PR will be included in the next version bump.

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

@hiendv hiendv force-pushed the type-generation-workflow-import branch 5 times, most recently from 9ec6d5b to 696c853 Compare June 24, 2025 02:36
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Jul 7, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jul 7, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@9705

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@9705

miniflare

npm i https://pkg.pr.new/miniflare@9705

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@9705

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@9705

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@9705

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@9705

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@9705

wrangler

npm i https://pkg.pr.new/wrangler@9705

commit: 615ca52

@hiendv hiendv force-pushed the type-generation-workflow-import branch from 696c853 to 75d976e Compare July 14, 2025 03:32
@penalosa
Copy link
Contributor

@sidharthachatterjee could you take a look at this?

@penalosa penalosa moved this from Untriaged to In Review in workers-sdk Jul 14, 2025
@LuisDuarte1 LuisDuarte1 self-assigned this Jul 21, 2025
@LuisDuarte1 LuisDuarte1 self-requested a review July 21, 2025 17:59
@LuisDuarte1 LuisDuarte1 removed their assignment Jul 21, 2025
@sidharthachatterjee
Copy link
Contributor

Thanks @penalosa

@LuisDuarte1 from the Workflows team will be reviewing this, cheers

Copy link
Contributor

@LuisDuarte1 LuisDuarte1 left a comment

Choose a reason for hiding this comment

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

While the current approach technically works, I would prefer your latter approach with GetWorkflowPayload that uses the infer param instead of introspecting the type of payload field, wdyt?

@hiendv
Copy link
Contributor Author

hiendv commented Aug 2, 2025

@LuisDuarte1 Got it. Just one more thing, since we have no access to workerd/worker.mjs afaik, is it acceptable for me to add the utility type to the type-generation?

@hiendv hiendv force-pushed the type-generation-workflow-import branch from 75d976e to 4df60c5 Compare August 2, 2025 06:28
@penalosa penalosa requested a review from LuisDuarte1 August 11, 2025 13:27
@penalosa penalosa force-pushed the type-generation-workflow-import branch from 4df60c5 to d737e5f Compare September 10, 2025 11:35
@amcclosky
Copy link

@LuisDuarte1 @penalosa just recently started using workflows and this PR would be a nice improvement.

@hiendv
Copy link
Contributor Author

hiendv commented Sep 23, 2025

Well, if workerd/worker.mjs is not really meant to be touched, I'm happy to move the utility type to the generated output. Just think workerd/worker.mjs suits better though.

@vicb
Copy link
Contributor

vicb commented Sep 29, 2025

@penalosa can we get this in (after rebase)?

@penalosa
Copy link
Contributor

@LuisDuarte1 are there concerns with the approach here?

@LuisDuarte1
Copy link
Contributor

LuisDuarte1 commented Sep 29, 2025

@LuisDuarte1 are there concerns with the approach here?

Would prefer to somehow hide the .['run']>[0]['payload']> part because that seems a bit verbose. But it's non-blocking and can be improved later.

@github-project-automation github-project-automation bot moved this from In Review to Approved in workers-sdk Sep 29, 2025
@penalosa
Copy link
Contributor

penalosa commented Sep 29, 2025

@hiendv in order to add the type helper, you'd have to make a PR to workerd modifying the workflows type definitions: https://github.com/cloudflare/workerd/blob/main/types/defines/workflows.d.ts. I'd be happy to review and get that released if you open one!

@hiendv
Copy link
Contributor Author

hiendv commented Oct 1, 2025

OK so I took a look at workerd and it seems more complicated than anticipated.
This is my proposal

  1. workerd/types/defines/rpd.d.ts (yes, not workflow.d.ts because WorkflowEntrypoint belongs here)
// After export abstract class WorkflowEntrypoint
type AnyWorkflowEntrypoint = new (...args: any) => WorkflowEntrypoint;
export type WorkflowPayload<T extends AnyWorkflowEntrypoint> = InstanceType<T> extends WorkflowEntrypoint<unknown, infer P> ? P : never;
  1. type-generation
interface Env {
		MY_WORKFLOW: Workflow<import('cloudflare:workers').WorkflowPayload<typeof import("./src/index").MyWorkflow>>;
	}

Doesn't seem better because of the import('cloudflare:workers'), to be honest. What do you think?

@hiendv hiendv force-pushed the type-generation-workflow-import branch from d737e5f to d318690 Compare October 1, 2025 14:14
@hiendv
Copy link
Contributor Author

hiendv commented Oct 1, 2025

  1. type-generation
interface Env {
		MY_WORKFLOW: Workflow<import('cloudflare:workers').WorkflowPayload<typeof import("./src/index").MyWorkflow>>;
	}

Doesn't seem better because of the import('cloudflare:workers'), to be honest. What do you think?

Lol, pipeline looks messy as well 😆

import("cloudflare:pipelines").Pipeline<import("cloudflare:pipelines").PipelineRecord>;

@petebacondarwin petebacondarwin force-pushed the type-generation-workflow-import branch from d318690 to 615ca52 Compare October 15, 2025 10:10
@petebacondarwin petebacondarwin merged commit 0ee1a68 into cloudflare:main Oct 15, 2025
32 of 33 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Oct 15, 2025
@workers-devprod workers-devprod added the contribution [Holopin] Recognizes an open-source contribution, big or small label Oct 15, 2025
@holopin-bot
Copy link

holopin-bot bot commented Oct 15, 2025

Congratulations @hiendv, the maintainer of this repository has issued you a holobyte! Here it is: https://holopin.io/holobyte/cmgs02iqy00c4l504gr4ixtz4

This badge can only be claimed by you, so make sure that your GitHub account is linked to your Holopin account. You can manage those preferences here: https://holopin.io/account.
Or if you're new to Holopin, you can simply sign up with GitHub, which will do the trick!

@hiendv hiendv deleted the type-generation-workflow-import branch October 17, 2025 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution [Holopin] Recognizes an open-source contribution, big or small workflows

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Stricter generated types for Workflows in worker-configuration.d.ts

8 participants