Skip to content

fix: cast TestConvex generic in test helper to satisfy workpool.register - #239

Closed
loudmu wants to merge 1 commit into
convex:mainfrom
loudmu:fix/test-helper-workpool-type-invariance
Closed

loudmu wants to merge 1 commit into
convex:mainfrom
loudmu:fix/test-helper-workpool-type-invariance

Conversation

@loudmu

@loudmu loudmu commented Apr 12, 2026 •

Copy link
Copy Markdown

Problem

Users who import @convex-dev/workflow/test and run convex dev (which invokes tsc on the convex directory) hit a TS2345 type error inside the package itself:

node_modules/@convex-dev/workflow/src/test.ts:17:21 - error TS2345:
Argument of type 'TestConvex<Schema>' is not assignable to parameter of type
'TestConvex<SchemaDefinition<GenericSchema, boolean>>'

Root cause

TypeScript generics are invariant by default. Inside register<Schema extends SchemaDefinition<...>>, the local t: TestConvex<Schema> is a constrained generic — it is not assignable to TestConvex<SchemaDefinition<GenericSchema, boolean>> (the concrete base type) even though Schema extends SchemaDefinition<GenericSchema, boolean>. This is standard TypeScript variance behaviour.

workpool.register expects the concrete base type, so passing the constrained generic t directly fails the type check.

Fix

Cast t to the concrete base type before passing it to workpool.register. The cast is safe at runtime — the object satisfies the interface regardless of which generic was inferred at the call site.

workpool.register(
  t as unknown as TestConvex<SchemaDefinition<GenericSchema, boolean>>,
  `${name}/workpool`,
);

Reproduction

Use @convex-dev/workflow@0.3.9 in a project that imports workflowTest from "@convex-dev/workflow/test" and run tsc --noEmit -p convex/tsconfig.json. The error appears on line 17 of src/test.ts.

Related

Closes / related to #28 (convex-test compatibility).

Summary by CodeRabbit

  • Chores
    • Updated internal type handling for compatibility without affecting user-facing functionality.

TestConvex<Schema> (where Schema is a constrained generic parameter) is
invariant with respect to Schema in TypeScript, so it is not assignable
to TestConvex<SchemaDefinition<GenericSchema, boolean>> even though
Schema extends SchemaDefinition<GenericSchema, boolean>.

This causes a TS2345 error inside register() when users import the test
helper and run convex's tsc typecheck against their own schema:

  error TS2345: Argument of type 'TestConvex<Schema>' is not assignable
  to parameter of type 'TestConvex<SchemaDefinition<GenericSchema, boolean>>'

Fix by casting t to the concrete base type before passing it to
workpool.register. The cast is safe at runtime — the object satisfies
the interface regardless of the generic parameter used at the call site.
@coderabbitai

coderabbitai Bot commented Apr 12, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c3f617b3-4d76-4bb1-894f-7c72fbc1c1a4

📥 Commits

Reviewing files that changed from the base of the PR and between 196ef80 and 3272d29.

📒 Files selected for processing (1)
  • src/test.ts

📝 Walkthrough

Walkthrough

The register function in src/test.ts was updated to apply an explicit type cast to the TestConvex instance before passing it to workpool.register. The cast routes the instance through unknown to TestConvex<SchemaDefinition<GenericSchema, boolean>>, modifying type compatibility without altering the runtime behavior of the registration call.

Changes

Cohort / File(s) Summary
Type Casting Update
src/test.ts
Added explicit type cast to TestConvex instance in the register function before passing it to workpool.register, improving type-level compatibility without changing runtime behavior.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 A type cast whispers through the code so clean,
No logic changed, just types refined between,
The workpool now knows what it receives,
Safe and sound in what it believes! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: adding a type cast to TestConvex generic in the test helper to resolve workpool.register type compatibility.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ianmacartney

Copy link
Copy Markdown
Member

Have you tried with the latest version of convex, which modifies the generic data model / schema type to be more assignable, and the latest version of convex-test which also updates TestConvex to not be bivariant in the Schema?

@pkg-pr-new

pkg-pr-new Bot commented Apr 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@convex-dev/workflow@239

commit: 3272d29

@ianmacartney

Copy link
Copy Markdown
Member

updating to the latest workpool, convex-test, convex, etc should fix it - otherwise if you could share the versions you're using via npm ls / a reproduction that would be helpful

@loudmu

loudmu commented Apr 15, 2026 via email

Copy link
Copy Markdown
Author

@ianmacartney

Copy link
Copy Markdown
Member

glad to hear it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants