diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..6461dee --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +*.ts diff --git a/src/internal/args.ts b/src/internal/args.ts index 3de3190..9181829 100644 --- a/src/internal/args.ts +++ b/src/internal/args.ts @@ -116,6 +116,9 @@ export interface WithFallbackConfig extends export const isArgs = (u: unknown): u is Args.Args => typeof u === "object" && u != null && ArgsTypeId in u +/** @internal */ +export const isInstruction = <_>(self: Args.Args<_>): self is Instruction => self as any + /** @internal */ export const isEmpty = (self: Instruction): self is Empty => self._tag === "Empty" @@ -131,6 +134,13 @@ export const isMap = (self: Instruction): self is Map => self._tag === "Map" /** @internal */ export const isVariadic = (self: Instruction): self is Variadic => self._tag === "Variadic" +/** @internal */ +export const isWithDefault = (self: Instruction): self is WithDefault => self._tag === "WithDefault" + +/** @internal */ +export const isWithFallbackConfig = (self: Instruction): self is WithFallbackConfig => + self._tag === "WithFallbackConfig" + // ============================================================================= // Constructors // ============================================================================= @@ -351,11 +361,10 @@ export const withFallbackConfig: { (config: Config.Config) => (self: Args.Args) => Args.Args, (self: Args.Args, config: Config.Config) => Args.Args >(2, (self, config) => { - if ((self as Instruction)._tag === "WithDefault") { - const withDefault = self as WithDefault + if (isInstruction(self) && isWithDefault(self)) { return makeWithDefault( - withFallbackConfig(withDefault.args, config), - withDefault.fallback as any + withFallbackConfig(self.args, config), + self.fallback as any ) } return makeWithFallbackConfig(self, config) diff --git a/src/internal/options.ts b/src/internal/options.ts index c060a53..53b4ca8 100644 --- a/src/internal/options.ts +++ b/src/internal/options.ts @@ -144,6 +144,9 @@ export interface WithDefault extends export const isOptions = (u: unknown): u is Options.Options => typeof u === "object" && u != null && OptionsTypeId in u +/** @internal */ +export const isInstruction = <_>(self: Options.Options<_>): self is Instruction => self as any + /** @internal */ export const isEmpty = (self: Instruction): self is Empty => self._tag === "Empty" @@ -165,6 +168,10 @@ export const isOrElse = (self: Instruction): self is OrElse => self._tag === "Or /** @internal */ export const isWithDefault = (self: Instruction): self is WithDefault => self._tag === "WithDefault" +/** @internal */ +export const isWithFallbackConfig = (self: Instruction): self is WithFallbackConfig => + self._tag === "WithFallbackConfig" + // ============================================================================= // Constructors // ============================================================================= @@ -540,11 +547,10 @@ export const withFallbackConfig: { (config: Config.Config) => (self: Options.Options) => Options.Options, (self: Options.Options, config: Config.Config) => Options.Options >(2, (self, config) => { - if ((self as Instruction)._tag === "WithDefault") { - const withDefault = self as WithDefault + if (isInstruction(self) && isWithDefault(self)) { return makeWithDefault( - withFallbackConfig(withDefault.options, config), - withDefault.fallback as any + withFallbackConfig(self.options, config), + self.fallback as any ) } return makeWithFallbackConfig(self, config)