1

The concept of yargs is nice.

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

console.log(argv);

But if I check argv.env explicit, typescript say: There is no argv.env. How can I solve this in typescript?

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

1 Answer 1

2

This work (if the program not using any asynchronous commands):

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, parseSync() method solved it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.