Repository navigation
Add typed resources with defineResource - #4
Merged
Merged
Conversation
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>
examples/basic imports @mutaflow/devtools, whose dist is missing on a clean CI checkout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resource keys can now carry a value type.
defineResource<Todo[]>("todos:list")returns aResourceKey<Todo[]>, which is the same string at runtime.The value type now reaches:
store.get/set/update/registeranduseResourceoptimistic.insert/update/remove/replacehelpers (the item type comes from the target)optimisticandreconcilecallbacks (currentisTodo[]instead ofunknown)getResource/setResource/expectResource/expectOptimisticState/expectRollback/expectReconcileduseOptimisticFlow, which now rejects state that does not match the flowPlain string keys still work, without a type.
Notable changes
OptimisticPatch. The helpers now return an optimistic config withoutonSuccess. Before this,optimistic.insert<Input, Todo>(...)with explicit type arguments made the flow's result typeunknown, soresulthad no fields inreconcile. A type test guards against this.anyinstead ofunknown. Callbacks with an untypedtargetnow getcurrent: anyinstead ofunknown. Without this, a typed flow could not be passed torunFlow/useFlow. The CHANGELOG notes this.targetis optional inoptimistic.*.useOptimisticFlowdoes not need it.createFlow,FlowConfig,FlowDefinitionandcreateNextSafeActionFlowtake optional state type parameters, inferred from typed targets.Type-level tests
tests/types/resources.tsholds@ts-expect-errorcases for wrong types, plus checks that fail if a type silently becomesany.npm run typecheck, so CI runs them too.examples/basic, which was never compiled before. It now uses a typedtodosResource.Test plan
npm run typecheck(packages,tests/typesandexamples/basic)npm test: 47/47examples/next-app: typecheck andnext build🤖 Generated with Claude Code