可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
回答2:
You can also do:
npm install --save @types/commander
回答3:
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!