withfig/autocomplete-tools

Enforce constraints through types instead of lints

Closed this issue · 1 comments

clo4 commented

Some lints (useless arrays, empty arrays) can be enforced through the type system instead of eslint.

The main disadvantage is error messages, and there's no quick-fix for these. For this reason, it's probably best to keep the lints. Just wanted to bring this up!


This type can be used instead of Option[], Subcommand[], etc:

/** An array containing at least one `T` */
export type NonEmptyArray<T> = [T, ...T[]];

This will ensure that only a single value or an array of two-or-more values is used:

export type SingleOrArray<T> = T | [T, T, ...T[]];

This would enforce that only the final argument is variadic (is this a constraint we care about? no lint for this at the moment, so I think not). The type is backwards compatible .

/** Either a `U` or an array containing `T`s that may end with `U` */
export type SingleOrArray<T, U extends T = T> =
	| U
	| [T, T, ...T[]]
	| [T, ...T[], U];

interface SingleArg {
  // current Arg, without isVariadic
}
interface Arg extends SingleArg {
  isVariadic?: boolean;
}
interface Option {
  args?: SingleOrArray<SingleArg, Arg>;
};

These will all require changes to specs as some code assumes SingleOrArray is the same as T | T[] (which, to be fair, it was)

clo4 commented

Thinking about this more, this would make it harder for other implementations of Fig specs to be a valid subset of them.

Instead, the canonical "fig schema" should be as broad as possible and opinions (such as how many elements an array should have) should only be enforced through linting.