-
Notifications
You must be signed in to change notification settings - Fork 564
Add TypeFactory types to tree-agent #26167
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| --- | ||
| "@fluidframework/tree-agent": minor | ||
| --- | ||
|
|
||
| tree-agent: New type factory system for method and property bindings | ||
|
Check warning on line 5 in .changeset/lemon-deer-walk.md
|
||
|
|
||
| The `@fluidframework/tree-agent` package now includes a custom type system (Type Factory) as an alternative to Zod for | ||
| defining method and property types. This new system is available in the `/alpha` entry point and provides a familiar | ||
| API for type definitions. | ||
|
|
||
| ## Key features | ||
|
|
||
| - **Familiar API**: Use `tf.string()`, `tf.object()`, etc. - similar to Zod's syntax (where `tf` is aliased from | ||
| `typeFactory`) | ||
| - **Same API surface**: The existing `expose`, `exposeProperty`, and `buildFunc` methods work with both Zod and Type | ||
| Factory types | ||
Josmithr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Usage | ||
|
|
||
| Import from the alpha entry point to use Type Factory types: | ||
|
|
||
| ```typescript | ||
| import { typeFactory as tf, buildFunc, exposeMethodsSymbol } from "@fluidframework/tree-agent/alpha"; | ||
| import { SchemaFactory } from "@fluidframework/tree"; | ||
|
|
||
| const sf = new SchemaFactory("myApp"); | ||
|
|
||
| class TodoList extends sf.object("TodoList", { | ||
| items: sf.array(sf.string), | ||
| }) { | ||
| public addItem(item: string): void { | ||
| this.items.insertAtEnd(item); | ||
| } | ||
|
|
||
| public static [exposeMethodsSymbol](methods) { | ||
| methods.expose( | ||
| TodoList, | ||
| "addItem", | ||
| buildFunc({ returns: tf.void() }, ["item", tf.string()]) | ||
| ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Available types | ||
|
|
||
| All common types are supported: | ||
|
|
||
| - **Primitives**: `tf.string()`, `tf.number()`, `tf.boolean()`, `tf.void()`, `tf.undefined()`, `tf.null()`, | ||
| `tf.unknown()` | ||
| - **Collections**: `tf.array(elementType)`, `tf.object({ shape })`, `tf.map(keyType, valueType)`, | ||
| `tf.record(keyType, valueType)`, `tf.tuple([types])` | ||
| - **Utilities**: `tf.union([types])`, `tf.literal(value)`, `tf.optional(type)`, `tf.readonly(type)` | ||
| - **Schema references**: `tf.instanceOf(SchemaClass)` | ||
|
|
||
| ## Migration from Zod | ||
|
|
||
| You can migrate gradually - both Zod and Type Factory types work in the same codebase: | ||
|
|
||
| **Before (Zod):** | ||
|
|
||
| ```typescript | ||
| import { z } from "zod"; | ||
| import { buildFunc, exposeMethodsSymbol } from "@fluidframework/tree-agent"; | ||
|
|
||
| methods.expose( | ||
| MyClass, | ||
| "myMethod", | ||
| buildFunc({ returns: z.string() }, ["param", z.number()]) | ||
| ); | ||
| ``` | ||
|
|
||
| **After (Type Factory):** | ||
|
|
||
| ```typescript | ||
| import { typeFactory as tf, buildFunc, exposeMethodsSymbol } from "@fluidframework/tree-agent/alpha"; | ||
|
|
||
| methods.expose( | ||
| MyClass, | ||
| "myMethod", | ||
| buildFunc({ returns: tf.string() }, ["param", tf.number()]) | ||
| ); | ||
| ``` | ||
|
|
||
| ## Note on type safety | ||
|
|
||
| The Type Factory type system does not currently provide compile-time type checking, though this may be added in the | ||
| future. For applications requiring strict compile-time validation, Zod types remain fully supported. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ Datastore | |
| DDSes | ||
| accessor | ||
| accessors | ||
| Zod | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.