Skip to content
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

chore(ts): tsify lib/command #1654

Merged
merged 8 commits into from
May 18, 2020
Merged

chore(ts): tsify lib/command #1654

merged 8 commits into from
May 18, 2020

Conversation

mleguen
Copy link
Member

@mleguen mleguen commented May 6, 2020

Follow up (and last PR) of the 2nd step of #1586: this PR tsifies the lib/middleware module and fixes some bugs revealed by type errors:

  • true was silently accepted as a command description, leading to displaying "true" as the description
  • some no longer used function parameters had not been removed
  • options.aliases was wrongly used as string instead of string[] in parseCommand, leading to the last positional alias only to be taken into account

@mleguen
Copy link
Member Author

mleguen commented May 6, 2020

To be rebased before review, once #1636 is reviewed and merged.

@mleguen mleguen force-pushed the 1586-ts-command branch 3 times, most recently from 7ec5e9b to 298e4fd Compare May 6, 2020 17:17
The documentation states addHandler can be called
either with a string description
or false to disable description display.
But true was also silently accepted,
which led to displaying 'true' as the description.
ParseCommand filled options.alias for positionals with a string
instead of a string[] as does yargs,
so only the last defined alias was provided to yargs-parser
and other aliases were silently ignored.
To avoid it to be seen as uncovered branches
@mleguen mleguen marked this pull request as ready for review May 13, 2020 08:43
@mleguen mleguen requested a review from bcoe May 13, 2020 08:43
export type Dictionary<T = any> = { [key: string]: T }

export type NotEmptyArray<T = any> = [T, ...T[]]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the idea that these types aren't specific to yargs data structures?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The main reason for this is split() signature is not precise enough: it returns string[] as if the first argument could be undefined, which is not the case. Its real signature should be [string, ...string[]].

When you are destructuring the results of split(), for example:

const [cmd, ...args] = something.split();

then cmd is string | undefined when it should be string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To answer more precisely your question: common-types was at first the place to put types several modules would use, but at the moment is mostly used by types which I think should be typescript standards, but are not yet implemented.

_hasParseCallback (): boolean
// TODO: to be precised once yargs is tsified
_parseArgs (args: null, shortCircuit: null, _calledFromCommand: boolean, commandIndex: number): Arguments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we make the next breaking change of yargs, should we migrate any of these _ methods to being private in TypeScript?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

The next breaking change in yargs should be to switch from factories to classes, and use private and public methods to distinguish inner and API methods.

This would ease a lot understanding, IDE tools use, type inference, etc.

@bcoe
Copy link
Member

bcoe commented May 15, 2020

@mleguen a couple questions, I'm quite happy with this though 👍

@bcoe bcoe merged commit ca59758 into master May 18, 2020
@bcoe bcoe deleted the 1586-ts-command branch May 18, 2020 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants