Generics
Keywords
This table includes all generic keywords available in default type
API.
Alias | Description |
---|---|
Exclude | exclude branches of a union like Exclude("boolean", "true") |
Extract | extract branches of a union like Extract("0 | false | 1", "number") |
Merge | merge an object's properties onto another like Merge(User, { isAdmin: "true" }) |
Omit | omit a set of properties from an object like Omit(User, "age") |
Partial | make all named properties of an object optional like Partial(User) |
Pick | pick a set of properties from an object like Pick(User, "name | age") |
Record | instantiate an object from an index signature and corresponding value type like Record("string", "number") |
Required | make all named properties of an object required like Required(User) |
Syntax
Generics can be declared and instantiated in one of three ways.
Definition
Constrained Parameters
All syntax in parameters definitions and all references to generic args are fully-type safe and autocompleted like any built-in keyword. Constraints can be used just like TS to limit what can be passed to a generic and allow that arg to be used with operators like >
.
Scoped
There is a special syntax for specifying generics in a scope:
Invocation
Chained
Invoked
HKT
Our new generics have been built using a new method for integrating arbitrary external types as native ArkType generics! This opens up tons of possibilities for external integrations that would otherwise not be possible. As a preview, here's what the implementation of Partial
looks like internally:
Recursive and cyclic generics are also currently unavailable and will be added soon.
For more usage examples, check out the unit tests for generics here.
External
The most basic pattern for wrapping a Type looks something like this:
For a deeper integration, you may wish to parse a definition directly:
The sky's the limit when it comes to this sort of integration, but be warned- TypeScript generics are notoriously finicky and you may find APIs like these difficult to write if you're not used to it.