-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.ts
100 lines (85 loc) · 2.04 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
export type TypeFunction<ReturnType = any> = (value: any) => ReturnType;
type TypeFunctionArray<ReturnType = any> = readonly [TypeFunction<ReturnType>];
type FlagSchemaBase<TF> = {
/**
Type of the flag as a function that parses the argv string and returns the parsed value.
@example
```
type: String
```
@example Wrap in an array to accept multiple values.
```
type: [Boolean]
```
@example Custom function type that uses moment.js to parse string as date.
```
type: function CustomDate(value: string) {
return moment(value).toDate();
}
```
*/
type: TF;
/**
A single-character alias for the flag.
@example
```
alias: 's'
```
*/
alias?: string;
} & Record<PropertyKey, unknown>;
type FlagSchemaDefault<TF, DefaultType = any> = FlagSchemaBase<TF> & {
/**
Default value of the flag. Also accepts a function that returns the default value.
[Default: undefined]
@example
```
default: 'hello'
```
@example
```
default: () => [1, 2, 3]
```
*/
default: DefaultType | (() => DefaultType);
};
export type FlagSchema<TF = TypeFunction | TypeFunctionArray> = (
FlagSchemaBase<TF>
| FlagSchemaDefault<TF>
);
export type FlagTypeOrSchema<
ExtraOptions = Record<string, unknown>
> = TypeFunction | TypeFunctionArray | (FlagSchema & ExtraOptions);
export type Flags<ExtraOptions = Record<string, unknown>> = {
[flagName: string]: FlagTypeOrSchema<ExtraOptions>;
};
export type InferFlagType<
Flag extends FlagTypeOrSchema
> = (
Flag extends (TypeFunctionArray<infer T> | FlagSchema<TypeFunctionArray<infer T>>)
? (
Flag extends FlagSchemaDefault<TypeFunctionArray<T>, infer D>
? T[] | D
: T[]
)
: (
Flag extends TypeFunction<infer T> | FlagSchema<TypeFunction<infer T>>
? (
Flag extends FlagSchemaDefault<TypeFunction<T>, infer D>
? T | D
: T | undefined
)
: never
)
);
export type TypeFlag<Schemas extends Flags> = {
flags: {
[flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
};
unknownFlags: {
[flag: string]: (string | boolean)[];
};
_: string[] & {
'--': string[];
};
};