GithubHelp home page GithubHelp logo

commander-js / extra-typings Goto Github PK

View Code? Open in Web Editor NEW
50.0 50.0 4.0 330 KB

Infer strong typings for commander options and action handlers

License: MIT License

JavaScript 2.67% TypeScript 97.33%

extra-typings's People

Contributors

abetomo avatar avivahl avatar edwardfoyle avatar shadowspawn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

extra-typings's Issues

Type inference isn't working for me

Based on the README, I would expect the options variable in the following code to be typed with red and blue fields, but it's actually typed as {}, both in WebStorm and when I compile and try to access the red field:

import { Command } from "@commander-js/extra-typings";

const program = new Command()
    .version("1.0.0")
    .option("-r --red", "Only change the pattern on the red goal")
    .option("-b --blue", "Only change the pattern on the blue goal")
    .command("rainbow")
        .action(async () => {
        });
const options = program.opts();

Versions

typescript: 5.0.2
commander: 10.0.1
extra-typings: 10.0.3

Revisit TypeScript definitions for dual cjs/esm

For background see: tj/commander.js#1880

Basically, we need a separate type definition file for each of cjs and esm.

Because this repo is all about the TypeScript, I suggest we add the esm.d.mts file beside the entry point and actually delete the "types" entry. Some TypeScript gurus recommend this and suggest the "types" entry is only needed for more complex scenarios when need to point to different folders.

https://arethetypeswrong.github.io/?p=%40commander-js%2Fextra-typings%4010.0.3

Argument type inference fails some permutations

The Argument chaining modifies a single ArgType each call, which feels far simpler than what Option does with lots of generic types. But it turns out Argument isn't coping with all cases. Found this #29 (comment)

const args = new Command()
  .addArgument(new Argument('foo').default('missing').argOptional())
  .parse()
  .processedArgs;
expectType<string>(args[0]); // FAILS because argOptional added back undefined

Ready for a first release?

I have got the package skeleton working, and quite a lot of arguments and option typing working. Ok to publish a version?

% npm publish --dry-run                                                                             main
npm notice 
npm notice ๐Ÿ“ฆ  @commander-js/[email protected]
npm notice === Tarball Contents === 
npm notice 1.1kB  LICENSE     
npm notice 790B   README.md   
npm notice 36.5kB index.d.ts  
npm notice 844B   index.js    
npm notice 946B   package.json
npm notice === Tarball Details === 
npm notice name:          @commander-js/extra-typings             
npm notice version:       0.1.0                                   
npm notice filename:      @commander-js/extra-typings-0.1.0.tgz   
npm notice package size:  10.2 kB                                 
npm notice unpacked size: 40.1 kB                                 
npm notice shasum:        95a2f1937534d1c2beaf52fa827e45b0bbf77e13
npm notice integrity:     sha512-MIYsGkrUAOSIw[...]wdYeQi1518pMQ==
npm notice total files:   5                                       
npm notice 
npm notice Publishing to https://registry.npmjs.org/ (dry-run)
+ @commander-js/[email protected]

Number parsing

Hello, and thanks for the library.

One problem with this library is that all of the options seem to be strings. Can you show an example in the README of how to use an option that is a number?

e.g.

const command = new Command()
  .command("init")
  .addOption(
    new Option(
      "-s, --slot <slot>",
      "The slot that you want to use.",
    ).choices(["1", "2", "3"]),
  )

const opts = command.opts();
console.log(typeof opt.slot); // "string" instead of "number"

InferOptions and friends are not exported

My program has wrapper objects for different Commands and I'm trying to type them appropriately. However, I can't write the necessary types because InferOptions and associated types aren't exported. I.e.

class Wrapper<Args, Opts> {
  constructor(private readonly cmd: Command<Args, Opts>) {}

  public specialOption(): Wrapper<Args, ???> { // Can't use InferOptions<Opts, ...> here
    return new Wrapper(this.cmd.option("--special"));
  }
}

infer or extrac args and opts type from program

I like to move my action handlers into a separate file and it would be nice if I could somehow infer the arguments and options types that are generated by just passing in an exported instance of the new Command() object. I see that I can pass in my own generic to the constructor but that kind of defeats the purpose of this library. Is there currently a way to do this?

extra-types is a production dependency

I didn't work out a way to make this a types-only package that could be installed into devDependencies, so probably worth clarifying in README that needs to be installed as a normal (production) dependency. It needs a small shim to map through onto the Commander implementation.

`createXXX` does not forward arguments

extra-typings/index.js

Lines 23 to 25 in 0d2d089

exports.createArgument = () => new commander.Argument();
exports.createCommand = () => new commander.Command();
exports.createOption = () => new commander.Option();

Despite their types being correct:

extra-typings/index.d.ts

Lines 1069 to 1071 in 0d2d089

export function createCommand(name?: string): Command;
export function createOption<Usage extends string>(flags: Usage, description?: string): Option<Usage>;
export function createArgument<Usage extends string>(name: Usage, description?: string): Argument<Usage>;

Action handler for asynchronous functions

Hi,
I found a type-warning when calling action-handler asynchronously.
In the commander.js code, the return value of the action handler is defined as void | Promise<void>.

commander.js
https://github.com/tj/commander.js/blob/release/10.x/typings/index.d.ts#L498

extra-typings
https://github.com/commander-js/extra-typings/blob/v10.0.2/index.d.ts#L667

//import { program } from 'commander';
import { program } from '@commander-js/extra-typings';

const actionHandler = async () => {
  // any code
};

program.command('sample').action(actionHandler); // ! type-warning
await program.parseAsync();

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.