Roaders/ts-command-line-args

Incorrect type when using "multiple: true"

biggianteye opened this issue · 1 comments

It looks like the correct type isn't being set when using the multiple: true option.

const args = parse({
    excludePaths: { type: String, multiple: true, optional: true },
});

// This line does not work
const paths: string[] = args.excludePaths;

The above minimal example config is a line taken from the README. My expectation is that args.excludePaths is of type string[] but when I try to assign it I get this error:

Type 'string' is not assignable to type 'string[]'.

If my expectation is wrong, what is the correct way of ensuring that the property is seen as an array?

If it helps to understand the problem, printing out the args object does show the property as an array:

{ excludePaths: [ 'foo' ] }

Actually, never mind. When I used an interface to enforce the types, it all compiles fine.

interface cliOptions {
    excludePaths: string[];
}
const args = parse<cliOptions>({
    excludePaths: { type: String, multiple: true, optional: true },
});

// This line now works
const paths: string[] = args.excludePaths;

My problem is resolved so I'll close this, but I'd be interested to understand why I need to declare type information in two separate places.