createShape
createShape<T>() builds a compile-time checked list of fields to request from the server. It validates dot-paths against T and produces a phantom type that represents the shaped response. Pair it with useData or useTypedData to append _shape=... to requests.
Source: src/helpers/shape-helper.ts
Why
- Keep responses small by requesting only what you need
- Maintain end-to-end type safety: invalid paths fail at compile time
- Works with nested objects and arrays (e.g.
Records.Items.NameorPosts.Title)
Usage
shape.fields through useData or directly pass the shape object to useTypedData:
Arrays and nesting
IfPosts is an array, 'Posts.Title' yields { Posts: { Title: string }[] } in shape.type.
Compile-time validation
Invalid paths produce a TypeScript error viaValidatePaths<T, Paths>.
End-to-end example
Best practices
- Define shapes next to the data hook or at top-level when reused across components
- Name exported shapes descriptively, e.g.
teamListShape - Keep shapes minimal; add fields only when needed by the UI