|
1 | | -import { KnownKey, MapUnion } from "./types" |
| 1 | +import { KnownKey, MapUnion } from "./types"; |
2 | 2 |
|
3 | | -type ArgType = "required" | "optional" |
| 3 | +type ArgType = "required" | "optional"; |
4 | 4 |
|
5 | | -type ArgTypeToValue<T extends ArgType> = MapUnion<T, { |
6 | | - required: string, |
7 | | - optional: string | undefined, |
8 | | -}>[T] |
| 5 | +type ArgTypeToValue<T extends ArgType> = MapUnion< |
| 6 | + T, |
| 7 | + { |
| 8 | + required: string; |
| 9 | + optional: string | undefined; |
| 10 | + } |
| 11 | +>[T]; |
9 | 12 |
|
10 | 13 | export type ParsedArgs<Args extends Record<string, Arg>> = { |
11 | | - [K in keyof Args as KnownKey<K>]: Args[K] extends Arg<infer T> ? ArgTypeToValue<T> : never |
12 | | -} |
| 14 | + [K in keyof Args as KnownKey<K>]: Args[K] extends Arg<infer T> |
| 15 | + ? ArgTypeToValue<T> |
| 16 | + : never; |
| 17 | +}; |
13 | 18 |
|
14 | 19 | export class Arg<T extends ArgType = ArgType> { |
15 | | - public constructor(private _type: T) { } |
16 | | - |
17 | | - public optional(): Arg<"optional"> { |
18 | | - this._type = "optional" as T |
19 | | - return this as Arg<"optional"> |
20 | | - } |
21 | | - |
22 | | - public static toObject(arg: Arg) { |
23 | | - return { |
24 | | - type: arg._type, |
25 | | - } |
26 | | - } |
27 | | - |
28 | | - public static toString(arg: Arg, name: string) { |
29 | | - if (arg._type === "required") return `<${name}>` |
30 | | - else return `[${name}]` |
31 | | - } |
| 20 | + public constructor(private _type: T) {} |
| 21 | + |
| 22 | + public optional(): Arg<"optional"> { |
| 23 | + this._type = "optional" as T; |
| 24 | + return this as Arg<"optional">; |
| 25 | + } |
| 26 | + |
| 27 | + public static toObject(arg: Arg) { |
| 28 | + return { |
| 29 | + type: arg._type, |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + public static toString(arg: Arg, name: string) { |
| 34 | + if (arg._type === "required") return `<${name}>`; |
| 35 | + else return `[${name}]`; |
| 36 | + } |
32 | 37 | } |
33 | 38 |
|
34 | 39 | export function arg() { |
35 | | - return new Arg("required") |
| 40 | + return new Arg("required"); |
36 | 41 | } |
0 commit comments