【问题标题】:How can I in typescript access to a explicit argument from yargs?如何在打字稿中访问来自 yargs 的显式参数?
【发布时间】:2021-08-02 15:53:05
【问题描述】:

yargs 的概念很好。

const argv = yargs.options({
    env: {
        alias: 'e',
        choices: ['dev', 'prod'] as const,
        demandOption: true,
        description: 'app environment'
    }
})
    .argv;

console.log(argv);

但如果我明确检查 argv.env,打字稿会说:没有 argv.env。如何在打字稿中解决这个问题?

if(argv.env == "dev"){ // not work in typescript
   ...
}

【问题讨论】:

标签: typescript yargs


【解决方案1】:

这项工作(如果程序不使用任何异步命令):

import yargs from 'yargs/yargs'; // <== very important !!!

const argv2 = yargs(process.argv.slice(2)).options({
    env: {
        alias: 'e',
        choices: ['dev', 'prod'] as const,
        demandOption: true,
        description: 'app environment'
    }
})
    .parseSync();

console.log(argv2.env);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2016-12-28
    • 2017-12-19
    • 2021-07-02
    • 2018-09-28
    相关资源
    最近更新 更多