> ⚠️ this README is out of date and describes the pre-Zod schema types. update plz
> ⚠️ this README is out of date and describes the pre-Zod schema types. update plz
Tool Kit allows plugins to take options from a .toolkitrc.yml file. Each
plugin has a different set of options that it can expect to get, and this
package defines the shape of these options. It does that via a schema object
defined for each of the plugins that take options. This is currently being used
primarily so that the user can be prompted for these options via the command
line interface used by the create package, though they could also be used to
verify options specified in a config file in the future.
Seeing as JavaScript does not have any runtime concept of types, we instead
define the types by string literals which can then be parsed at runtime
(allowances have been made to make parsing easier so some syntax forms might
look a little odd.) For example, a schema could look like this:
``typescript`
const ExampleSchema = {
files: 'string',
retryCount: 'number?'
} as const
which would define a set of options where it is expected a files string willretryCount
be specified, and a number field can be optionally specified for a .
In this table, A can be substituted by any of the first four typesB
(unfortunately TypeScript does not seem to support recursive conditional types
right now.) and C in the union type are string literals. T can be any?
type, with a suffix denoting an optional type.
| Syntax | Type |
| ------------ | ------------------- |
| "string" | string |"number"
| | number |"boolean"
| | boolean |"unknown"
| | unknown |"array.A"
| | A[] |"record.A"
| | Record |"\|B,C"
| | B \| C |"T?"
| | T \| undefined |
All schemas should use the TypeScript type Schema. This will make sure thatSchemaOutput
there aren't any typos in the schema and that is structured correctly. It also
allows you to make use of the type, a special generic type thatExampleSchema
can be used to extract the type of the object that is successfully validated by
the schema. For instance, you could use the type inferred by the SchemaOutput
declaration above as the type parameter for like so:
`typescript`
type ExampleOutput = SchemaOutput
which would create a type equivalent to
`typescript`
type ExampleOutput = {
files: string
retryCount?: number
}
This can be very useful when you don't want to have your options defined in two
different forms which you have to remember to keep in sync with each other
whenever you make any changes!
One gotcha to look out for is to make sure you cast any schema object you define
as constant with the as const assertion. This restricts TypeScript from"number"
'widening' the types, i.e., treating a literal field with value as astring rather than as having type "number", which the SchemaOutput depends
on for its conditional typing.
The Task class, which all tasks must inherit, takes a Schema as a typeSchemaOutput
parameter. It then uses to ascertain the type of the options thatSchema
are actually stored for the task. You should pass a to Task if your
plugin takes options, but it is safe to ignore this type parameter if your
plugin does not.
You should also follow the convention of storing your schema at , and export it from the module asSchema. This allows the create` package to dynamically read the schema and
prompt the user for options to set when they are initialising Tool Kit and its
plugins.