Skip to content

Commit

Permalink
Simpler result type
Browse files Browse the repository at this point in the history
  • Loading branch information
febeling committed Dec 7, 2023
1 parent 09f30f6 commit 7d213d7
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/synopt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ type Options = {
[key: string]: boolean | string | string[];
};

type ErrorResult = { ok: false; error: string };
type OkResult<T> = { ok: true; options: T };
type Result<T> = OkResult<T> | ErrorResult;
type Result<T> = { ok: false; error: string } | { ok: true; options: T };

const Ok = <T>(options: T): OkResult<T> => ({ ok: true, options });
const Ok = <T>(options: T): Result<T> => ({ ok: true, options });

const Err = (error: string): ErrorResult => ({
const Err = <T>(error: string): Result<T> => ({
ok: false,
error,
});
Expand Down Expand Up @@ -140,13 +138,13 @@ const createCommand = (name?: string): Command => {
* @param {object} [options] - Options for this declaration. Example `{ boolean: true, required: true }`
* @return {Command} The option declaration representation object
*/
option: (...args): Command => {
option: (...args: DeclarationTuple): Command => {
const declaration = parseDeclaration(args);
ensureNamesUnique(declaration, state.optionDeclarations);
state.optionDeclarations.push(declaration);
return command;
},
parse: (args): Result<Options> => {
parse: (args: string[]): Result<Options> => {
const options: Options = {};

try {
Expand Down

0 comments on commit 7d213d7

Please sign in to comment.