Surface / foldkit-surface

Field references

Surface.application generates a reference tree from your Model Schema, one node per field. A ref's string keys are strictly its child fields, so Model fields may use any name (index, at, get, select). A ref's own metadata lives under a symbol accessed via ModelRef.focus: its optic, codec, dependency path, get, set, and literal field name.

ts
App.model.todos                                // FieldRef<Model, Todo[], 'todos'>
App.model.selectedTodoId                       // FieldRef<Model, Option<string>, 'selectedTodoId'>
ModelRef.focus(App.model.todos).dependency     // ['todos']
ModelRef.focus(App.model.todosById).at('t1')   // OptionalRef<Model, Option<Todo>> (dynamic key)
ModelRef.focus(App.model.todos).index(0)       // OptionalRef<Model, Option<Todo>> (dynamic index)

Nested Schema.Struct fields recurse into child FieldRefs. Accessing an array with index(i) or a record with at(key) returns an OptionalRef (ModelRef yielding an Option). Because those lookups are dynamic, Projection.pick requires static field names and refuses them, though you can use them inside read-only Projections. (App.fields is a deprecated alias for App.model; prefer App.model.)

Projection.pick turns static references into a writable projection holding { schema, dependencies, get, set }:

ts
const Shared = Projection.pick(App.model.todos, App.model.selectedTodoId)
// { schema, dependencies, get, set }

Each picked field uses its final key name (App.model.post.id becomes id), so picking two fields that share a leaf name (post.id and viewer.id) throws; place them in separate projections instead. A per-application owner token prevents mixing references across two structurally identical Models. Duplicate members deduplicate, conflicting definitions throw, Projection.compose merges disjoint writable projections, and ModelRef.fromOptic builds a reference from an existing Effect optic.