Skip to main content

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.Name or Posts.Title)

Usage

Then pass shape.fields through useData or directly pass the shape object to useTypedData:

Arrays and nesting

If Posts is an array, 'Posts.Title' yields { Posts: { Title: string }[] } in shape.type.

Compile-time validation

Invalid paths produce a TypeScript error via ValidatePaths<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

See also