New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better Typescript documentation. #1906
Comments
@bcoe Why would you need to maintain them, isn't the project written in TS? Am I making a mistake here, or are you expected to work with untyped args? |
The type definitions in
I honestly haven't played much with the advanced features of My intention is to play with the feature more deeply, and document the current behavior. If there end up being any shortcomings or feature requests, they can be fixed on DefinitelyTyped. |
Make sure you https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/yargs/yargs-tests.ts Edit: also, saw your request to contribute in the v17.0.0 issue, would greatly appreciate your help documenting the current behavior. |
@bcoe That being said I did manage to get what I was looking for (THANKS!) by making a wrapper. export function createAdd(arg: Yargs.Argv): Yargs.Argv {
return arg.command("add <name> [url]", "sample stuff", (addYarg) => {
return addYarg
.positional("name", { type: "string" }, demandOption: true)
.positional("url", { type: "string" })
}, (args) => {
//args.name & args.url will be typed ✅
})
} While it does not work (out of the box) with const sourceAddCommand: Yargs.CommandModule = {
command: ["add <name> [url]"],
builder: (args) => {
return args
.positional("name", { type: "string", demandOption: true })
.positional("url", { type: "string" })
},
handler: (args) => {
//args & args.url ARE NOT typed ❌
},
} However there is partial support for just options and not positional. const sourceListCommand: Yargs.CommandModule<{}, {
"available": {
boolean: true,
conflicts: "outdated",
description: "Not visible ❌"
desc: "Not visible ❌",
}, "outdated": {
boolean: true,
conflicts: "available",
description: "Not visible ❌"
desc: "Not visible ❌"
}
}> = {
describe: "List Command",
command: "list",
handler: (args) => {
// args.available & args.outdated are typed ✅
},
} To get typed arguments and avoid polluting the argument interface from other sub-commands in the same parent you need to wrap the entire sub-command in a function. Yargs
.default(YargsHelper.hideBin(process.argv))
.scriptName(ProjectPackage.name)
.command("mother <command>", "One of the main commands", (yargs) => {
return [yargs]
.map(makeFirstSubCommand)
.map(createAdd)
[0]
})
. command("father <command>", "Other main command with its own subs", (yargs) => {
return [yargs]
.map(createAnotherSubCommandUnderFather)
.map(someMoreUnderFather)
[0]
})
.argv |
I am closing this in favour of #1899 - thanks for your time! |
I would love to see how to keep things DRY while retaining type information for arguments and options.
Doing something like what's mentioned here: #1905
With the
CommandModule<T, U>
approach I have to manage both types and options manuallyWhile the types are inferred from the builder using in-place adding of commands, I get all previous arguments and options.
Is there any documentation or reading material to clear these things up?
The text was updated successfully, but these errors were encountered: