Skip to content

Commit d8732a2

Browse files
committed
chore: stronger type assertion, fix erasable syntax
1 parent 7455b83 commit d8732a2

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

packages/arg-parser/src/arg-parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ describe('arg-parser', function () {
13811381
});
13821382

13831383
describe('parseArgs', function () {
1384-
it('passes any schema, independent of CliOptionsSchema', function () {
1384+
it('parses any schema, independent of CliOptionsSchema', function () {
13851385
const options = parseArgs({
13861386
args: [
13871387
'hello',

packages/arg-parser/src/arg-parser.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ import {
1616
UnsupportedCliArgumentError,
1717
} from './arg-metadata';
1818

19-
function unwrapType(type: unknown): unknown {
20-
if (type instanceof z.ZodOptional) {
19+
function unwrapType(type: unknown): z.ZodType {
20+
if (type instanceof z.ZodOptional || type instanceof z.ZodDefault) {
2121
return unwrapType(type.unwrap());
2222
}
23-
if (type instanceof z.ZodDefault) {
24-
return unwrapType(type.unwrap());
23+
if (!(type instanceof z.ZodType)) {
24+
throw new Error(
25+
`Unknown schema field type: ${
26+
type && typeof type === 'object' ? type.constructor.name : typeof type
27+
}`
28+
);
2529
}
2630
return type;
2731
}
@@ -49,20 +53,20 @@ export function generateYargsOptionsFromSchema({
4953
schema: z.ZodObject;
5054
parserOptions?: Partial<YargsOptions>;
5155
}): YargsOptions {
52-
const options = {
53-
...parserOptions,
54-
string: <string[]>[],
55-
boolean: <string[]>[],
56-
array: <string[]>[],
57-
alias: <Record<string, string>>{},
58-
coerce: <Record<string, (value: unknown) => unknown>>{},
59-
number: <string[]>[],
60-
} satisfies Required<
56+
const options: Required<
6157
Pick<
6258
YargsOptions,
6359
'string' | 'boolean' | 'array' | 'alias' | 'coerce' | 'number'
64-
>
65-
>;
60+
> & { array: string[] }
61+
> = {
62+
...parserOptions,
63+
string: [],
64+
boolean: [],
65+
array: [],
66+
alias: {},
67+
coerce: {},
68+
number: [],
69+
};
6670

6771
for (const [fieldName, fieldSchema] of Object.entries(schema.shape)) {
6872
const meta = getArgumentMetadata(schema, fieldName);

0 commit comments

Comments
 (0)