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

TypeScript definitions #27

Closed
tunnckoCore opened this issue Jun 23, 2019 · 6 comments
Closed

TypeScript definitions #27

tunnckoCore opened this issue Jun 23, 2019 · 6 comments

Comments

@tunnckoCore
Copy link

Ooops, one more issue :D

I may PR soon.

@tunnckoCore
Copy link
Author

tunnckoCore commented Jun 23, 2019

I'm not good at TS but something like that would work?

declare module 'sade' {
  interface CommandOptions {
    default: boolean;
  }

  interface MriOptions {
    alias: { [key: string]: any };
    string: string[];
    boolean: string[];
    default: { [key: string]: any };
  }

  type ParseOptions = { lazy: boolean } & MriOptions;

  type Action = (...args: any) => any;

  interface Sade {
    command(str: string, desc: string, opts: CommandOptions): Sade;
    describe(str: string): Sade;
    option(str: string, desc: string, val: any): Sade;

    action(handler: Action): Sade;

    example(str: string): Sade;

    version(str: string): Sade;

    parse(arr: string[], opts: ParseOptions): any;
  }
}

@lukeed
Copy link
Owner

lukeed commented Jun 23, 2019

For a project like sade, I would much rather they live inside DefintelyTyped, espeically since mri's are already there.

The sade types can extend and require mri's, which is the main reason for having them live there. If it were to live in-house, the types would be duplicated and probably inconsistent. Much better to have them resolve from the same place 👍

If sade had no dependencies, I'd definitely ship them in this repo.

If you want to PR into DT, I'll help review and get them in!

@Epimodev
Copy link

Hello,
Is there any update about PR into DefintelyTyped ?
If no one plans to do it in a few days, I can make a PR.

@Epimodev
Copy link

Hello @lukeed

I've just made a PR into DefintelyTyped for TypeScript users: DefinitelyTyped/DefinitelyTyped#40398

I check the documentation and the source code to create it but I may have made some mistakes.
Can you review it, please?

@Epimodev
Copy link

The PR was already reviewed and merged by a DefinitelyTyped maintainer.
So we can download types from npm: https://www.npmjs.com/package/@types/sade.

It works well for me but I still interested in having a review from you.

@lukeed
Copy link
Owner

lukeed commented Dec 31, 2021

Closed via 9171cbf Sade will ship its own type definitions starting with 1.8.0

Thank you to @Epimodev who filled in w/ the @types/sade package in the interim :)

Most notable difference (I think) is that your commands' action handlers can now have their arguments typed directly, allowing you to save/reuse them. It also always injects the mri.Argv type for the last options argument:

sade('hello')
    .command('foobar <name> [age]', 'asd', {
        default: true
    })
    .action<[string, number|null]>((name, age, options) => {
        name.length;
        // ^? type: string
        age
        // ^? type: number | null
        options
        // ^? type: mri.Argv<Default>
    })
    // ...

@lukeed lukeed closed this as completed Dec 31, 2021
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

No branches or pull requests

3 participants