Roaders / ts-command-line-args

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

Property 'optional' is missing in type '{ type: BooleanConstructor; alias: string; description: string; }' but required in type '{ optional: true; }'. #9

Closed ankopein closed 3 years ago

ankopein commented 3 years ago

Versions:

"ts-command-line-args": "^1.8.0",
"typescript": "^4.2.3"

I'm trying to specify a very simple cmd-line with 3 required args (2 strings, a bool and an optional bool (help)), but it fails to compile with tsc. I have no idea why, since I tried to stick with the README info. Is this a bug, or just incorrect usage? If its the 2nd, can you provide some explaination and please correct me? Thanks.

index.ts:14:9 - error TS2322: Type '{ type: BooleanConstructor; alias: string; description: string; }' is not assignable to type 'PropertyOptions'. Property 'optional' is missing in type '{ type: BooleanConstructor; alias: string; description: string; }' but required in type '{ optional: true; }'.

14 project: { type: Boolean, alias: "p", description: "do it project wide" },

interface CmdLineArgs {
    project: boolean;
    first: string;
    second: string;
    help?: boolean;
}
export const args = parse<CmdLineArgs>(
    {
        project: { type: Boolean, alias: "p", description: "do it project wide" },
        first: { type: String, alias: "f", description: "First input file" },
        second: { type: String, alias: "s", description: "Second input file" },
        help: { type: Boolean, optional: true, alias: "h", description: "Prints this usage guide" },
    },
    {
        helpArg: "help",
        headerContentSections: [{ header: "Usage", content: "node index.js [options] \n\nExec program. " }]
    },
);
Roaders commented 3 years ago

Can you share a repo that demonstrates the issue? I just created a whole new project, installed the versions that you state and copied your code and it's all good.

ankopein commented 3 years ago

Thanks for your quick response! I added you to a repo showing a minimal complete example. npm run startimmediatelyworks, npm start does not.

Roaders commented 3 years ago

I think it's the way you are running tsc. Remove the files: "prestart": "tsc" tsc will compile all ts files. If you want to specify which ones you want to compile then set it up in tsconfig.json I think that if you specify the files like that it might ignore the tsconfig.json in the root of the project.

ankopein commented 3 years ago

You're right, thanks!