Skip to content

Add typed resources with defineResource - #4

Merged
javaquasar merged 2 commits into
mainfrom
claude/typed-resources
Dec 2, 2026
Merged

javaquasar merged 2 commits into
mainfrom
claude/typed-resources

Conversation

@javaquasar

@javaquasar javaquasar commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

Resource keys can now carry a value type. defineResource<Todo[]>("todos:list") returns a ResourceKey<Todo[]>, which is the same string at runtime.

The value type now reaches:

  • store.get/set/update/register and useResource
  • the optimistic.insert/update/remove/replace helpers (the item type comes from the target)
  • the optimistic and reconcile callbacks (current is Todo[] instead of unknown)
  • the testkit helpers getResource/setResource/expectResource/expectOptimisticState/expectRollback/expectReconciled
  • useOptimisticFlow, which now rejects state that does not match the flow

Plain string keys still work, without a type.

export const todosResource = defineResource<Todo[]>("todos:list");

reconcile: {
  target: todosResource,
  onSuccess: (current, result) => current.map((todo) => ...), // current: Todo[]
},

Notable changes

  • OptimisticPatch. The helpers now return an optimistic config without onSuccess. Before this, optimistic.insert<Input, Todo>(...) with explicit type arguments made the flow's result type unknown, so result had no fields in reconcile. A type test guards against this.
  • any instead of unknown. Callbacks with an untyped target now get current: any instead of unknown. Without this, a typed flow could not be passed to runFlow/useFlow. The CHANGELOG notes this.
  • target is optional in optimistic.*. useOptimisticFlow does not need it.
  • State type parameters. createFlow, FlowConfig, FlowDefinition and createNextSafeActionFlow take optional state type parameters, inferred from typed targets.

Type-level tests

  • tests/types/resources.ts holds @ts-expect-error cases for wrong types, plus checks that fail if a type silently becomes any.
  • They run in npm run typecheck, so CI runs them too.
  • The same config also compiles examples/basic, which was never compiled before. It now uses a typed todosResource.

Test plan

  • npm run typecheck (packages, tests/types and examples/basic)
  • npm test: 47/47
  • examples/next-app: typecheck and next build

🤖 Generated with Claude Code

defineResource<T>(key) returns a ResourceKey<T>, a branded string that
carries the value type into the resource store, useResource, the
optimistic helpers, optimistic/reconcile callbacks, testkit assertions
and useOptimisticFlow. Plain string keys keep working untyped.

The optimistic helpers now return OptimisticPatch (no onSuccess), so
explicit helper type arguments no longer erase the flow result type.
Type-level tests in tests/types run in npm run typecheck and also
compile examples/basic, migrated to a typed todos resource.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javaquasar
javaquasar changed the base branch from claude/use-optimistic-flow to main December 2, 2026 14:15
examples/basic imports @mutaflow/devtools, whose dist is missing on a clean CI checkout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javaquasar
javaquasar merged commit 7da83c2 into main Dec 2, 2026
3 checks passed
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.

1 participant