Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dotcom-tool-kit/types

Package Overview
Dependencies
Maintainers
2
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dotcom-tool-kit/types

## Overview

  • 1.4.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
814
increased by37.27%
Maintainers
2
Weekly downloads
 
Created
Source

Tool Kit Option Schemas

Overview

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:

const ExampleSchema = {
  files: 'string',
  retryCount: 'number?'
} as const

which would define a set of options where it is expected a files string will be specified, and a number field can be optionally specified for a retryCount.

Schema Type Reference

In this table, A can be substituted by any of the first four types (unfortunately TypeScript does not seem to support recursive conditional types right now.) B and C in the union type are string literals. T can be any type, with a ? suffix denoting an optional type.

SyntaxType
"string"string
"number"number
"boolean"boolean
"unknown"unknown
"array.A"A[]
"record.A"Record<string, A>
"|B,C"B | C
"T?"T | undefined

TypeScript Helpers

All schemas should use the TypeScript type Schema. This will make sure that there aren't any typos in the schema and that is structured correctly. It also allows you to make use of the SchemaOutput type, a special generic type that 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 ExampleSchema declaration above as the type parameter for SchemaOutput like so:

type ExampleOutput = SchemaOutput<typeof ExampleSchema>

which would create a type equivalent to

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 'widening' the types, i.e., treating a literal field with value "number" as a string rather than as having type "number", which the SchemaOutput depends on for its conditional typing.

Integrating Into Code

The Task class, which all tasks must inherit, takes a Schema as a type parameter. It then uses SchemaOutput to ascertain the type of the options that are actually stored for the task. You should pass a Schema 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 <tool kit root>/lib/types/schema/<package name>.ts, and export it from the module as Schema. 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.

FAQs

Package last updated on 28 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc