Skip to content

Commit 65bbcad

Browse files
add a type guard for InputOutputGeneratorOptions and rewrite getGeneratorDefinition with that
Hopefully this reads a bit better and while it isn't a discriminated union (and while there's room for improvement still), it's at least not a type assertion
1 parent 9410256 commit 65bbcad

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,25 @@ const hashedString = (value: string) => {
9292
};
9393

9494
const getGeneratorDefinition = (
95-
value: GeneratorOptions | InputOutputGeneratorOptions,
95+
opts: GeneratorOptions | InputOutputGeneratorOptions,
9696
generatorMode: Options['generatorMode'],
9797
): GeneratorDefinition => {
98-
if (typeof value === 'string') {
98+
if (isAnInputOutputGeneratorOptions(opts)) {
99+
return buildGeneratorDefinition(opts[generatorMode]);
100+
}
101+
102+
return buildGeneratorDefinition(opts);
103+
};
104+
105+
const buildGeneratorDefinition = (opts: GeneratorOptions) => {
106+
if (typeof opts === 'string') {
99107
return {
100-
generator: value,
108+
generator: opts,
101109
arguments: [],
102110
};
103111
}
104112

105-
if (value !== undefined && 'input' in value && 'output' in value) {
106-
return getGeneratorDefinition(value[generatorMode], generatorMode);
107-
}
108-
109-
return value as GeneratorDefinition;
113+
return opts;
110114
};
111115

112116
const getCasualCustomValue = (
@@ -590,6 +594,11 @@ type InputOutputGeneratorOptions = {
590594
output: GeneratorOptions;
591595
};
592596

597+
const isAnInputOutputGeneratorOptions = (
598+
opts: GeneratorOptions | InputOutputGeneratorOptions,
599+
): opts is InputOutputGeneratorOptions =>
600+
opts !== undefined && typeof opts !== 'string' && 'input' in opts && 'output' in opts;
601+
593602
type ScalarMap = {
594603
[name: string]: GeneratorOptions | InputOutputGeneratorOptions;
595604
};

0 commit comments

Comments
 (0)