Utilities
Superstruct also ships with a handful of utility type factories, which allow you to easily manipulate and transforms existing structs.
assign
assign
assign
creates a new struct by mixing the properties of existing object structs, similar to JavaScript's native Object.assign
. It can accept object
and type
structs, returning a struct matching whichever is passed in as the first parameter (ie, which struct is being "assigned" into).
deprecated
deprecated
deprecated
structs validate that a value matches a specific struct, or that it is undefined
. But in addition, when the value is not undefined
, it will call the log
function you pass in so you can warn users that they're using a deprecated API.
dynamic
dynamic
dynamic
allows you to create a struct with validation logic that can change at runtime. The callback will be called with (value, context)
and must return the struct to continue validation with.
lazy
lazy
lazy
allows you to create a self-referential struct, useful for defining recursive data structures.
🤖 Note that TypeScript can't automatically infer the type from this kind of recursive structure, so you'll need to pass in the type manually.
omit
omit
omit
allows you to create a new struct based on an existing object
or type
struct, but excluding specific properties.
partial
partial
partial
allows you to create a new struct based on an existing object
or type
struct, but with all of its properties being optional.
pick
pick
pick
allows you to create a new struct based on an existing object
or type
struct, but only including specific properties.
Last updated