Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use commander (npm) with TypeScript?

I have the following code on my app:

import commander = require('commander');

commander
  .option('-u, --user [user]', 'user code')
  .option('-p, --pass [pass]', 'pass code')
  .parse(process.argv);

Then I try to access:

commander.user

But I get an error (commander.d.ts from DefinitelyTyped):

user does not exist on type IExportedCommand

I tried adding this

interface IExportedCommand {
  user: string;
  pass: string;
}

But I still get the error. How can I fix this?

like image 254
BrunoLM Avatar asked May 13 '16 01:05

BrunoLM


People also ask

What is npm commander?

You write code to describe your command line interface. Commander looks after parsing the arguments into options and command-arguments, displays usage errors for problems, and implements a help system.

Can you use node with TypeScript?

You can run typescript directly on the node with the ts-node package. This package is recommended for development only. To make the final deploy in production, always use the javascript version of your project. The ts-node is already included as a dependency on another package, t ts-node-dev .

How do I compile typescript in NPM?

In a terminal window, navigate to the source code folder you created. Then, run the following command: npm run compile-typescript Now, you should now see a new folder created named output, that contains one file named script.js. Notice how the output has defaulted to ES5 JavaScript, which is compatible with all major browsers.

What scripts can I use in NPM?

You can use any of these scripts in npm: Prepare will run before the package is packed and published, and also after it is installed. prepublish Only will run before preparation and only when you publish npm. Perversion will run before you upgrade to a new package version.

How do I combine the NPM start and compile-typescript commands?

To save time the "compile-typescript" and "start" commands can be combined into one command by modifying the start command to include this functionality. Now running the command npm run start will first run the "compile-typescript" command and then use node to run the script.js file that is output.

How do I run a typescript script with Node JS?

The script.js created as a result of running the "compile-typescript" command can now be run with Node.js. To do this another package.json script is added, which is named "start". The "start" script will run the node cli command which the path of the script.ts file is passed.


Video Answer


3 Answers

You can also do:

npm install --save-dev @types/commander

EDIT: Commander now includes its own TypeScript typings, so this should no longer be necessary.

like image 74
gpresland Avatar answered Oct 03 '22 16:10

gpresland


Create a file commander-expansion.d.ts with the following :

declare namespace commander {
    interface IExportedCommand extends ICommand {
        user: string; 
        pass: string;
    }
}

Tip

Because I did something like this recently, recommend --auth user:password. Saves you dealing with user missing username or password. But prevents using : as a password property

¯\_(ツ)_/¯

More : https://github.com/alm-tools/alm/blob/master/src/server/commandLine.ts#L24

like image 44
basarat Avatar answered Oct 03 '22 16:10

basarat


You can keep the Typescript interface within the same file.

interface InterfaceCLI extends commander.ICommand {
  user?: string
  password?: string
}

After you can define this interface to a variable after running the program.parse function.

const cli: InterfaceCLI = program.parse(process.argv)
console.log(cli.user, cli.password)

I hope this helps!

like image 37
user1116928 Avatar answered Oct 03 '22 17:10

user1116928