Roaders / ts-command-line-args

A typescript wrapper for command-line-args that allow you to generate config from existing TS interfaces
28 stars 11 forks source link

`optional: true` is required can't use `optional: false` #28

Closed aubelscirclein closed 2 years ago

aubelscirclein commented 2 years ago

This works

interface IArgs{
    forYear: number;
    forMonth: number;
    awsProfile?: string;
}

export const args = parse<IArgs>({
    forYear: { type: Number, optional: true },
    forMonth: { type: Number, optional: true },
    awsProfile: { type: String, optional: true },
}, {});

But this doesn't:

interface IArgs{
    forYear: number;
    forMonth: number;
    awsProfile?: string;
}

export const args = parse<IArgs>({
    forYear: { type: Number, optional: false },
    forMonth: Number,
    awsProfile: { type: String, optional: true },
}, {});
aubelscirclein commented 2 years ago
        "ts-command-line-args": "^2.2.1"
        "typescript": "^4.5.4",
Roaders commented 2 years ago

This is expected. The args object that you have to create is just a run time version of the interface that you define at compile time. It's shape is strictly govorned by the conditional types so it specifies that if it's optional you must have optional: true not optional: boolean. I could define optional?: false on the non optional type but that would further complicate a complicated area.

aubelscirclein commented 2 years ago

How would I know this without looking at the typescript defintions?