Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Schniz/cmd-ts. 💻 A type-driven command line argument parser cmd-ts.now.sh
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import type { Default, OnMissing } from "./default";import type { From, FromFn, InputOf, OutputOf } from "./from";import type { Descriptive, Displayed } from "./helpdoc";
export { identity, type OutputOf, type InputOf } from "./from";
export type Type<From_, To> = From<From_, To> & Partial<Descriptive & Displayed & Default<To> & OnMissing<To>>;
/** * Get the type definitions or an empty object from a type or a decoding function */export function typeDef<T extends From<any, any> | FromFn<any, any>>( from: T,): T extends FromFn<any, any> ? {} : Omit<T, "from"> { if (typeof from === "function") { return {} as any; } return from as any;}
/** * Get the decoding function from a type or a function */export function fromFn<A, B>(t: FromFn<A, B> | From<A, B>): FromFn<A, B> { if (typeof t === "function") { return t; } return t.from;}
/** * Extend a type: take a type and use it as a base for another type. Much like using the spread operator: * ``` * const newType = { ...oldType } * ``` * but composes the `from` arguments * * @param base A base type from `InputA` to `OutputA` * @param nextTypeOrDecodingFunction Either an entire `Type<OutputA, AnyOutput>` or just a decoding function from `OutputA` to any type */export function extendType< BaseType extends Type<any, any>, NextType extends | Type<OutputOf<BaseType>, any> | FromFn<OutputOf<BaseType>, any>,>( base: BaseType, nextTypeOrDecodingFunction: NextType,): Omit<BaseType, "from" | "defaultValue" | "onMissing"> & (NextType extends FromFn<any, any> ? unknown : Omit<NextType, "from">) & From<InputOf<BaseType>, OutputOf<NextType>> { const { defaultValue: _defaultValue, onMissing: _onMissing, from: _from, ...t1WithoutDefault } = base; const t2Object = typeDef(nextTypeOrDecodingFunction); const t2From = fromFn(nextTypeOrDecodingFunction);
return { ...t1WithoutDefault, ...t2Object, async from(a) { const f1Result = await base.from(a); return await t2From(f1Result); }, };}
/** Contains a type definition inside */export type HasType<T extends Type<any, any>> = { /** The value decoding strategy for this item */ type: T;};