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 Typings #65

Closed
jvilk opened this issue Aug 27, 2016 · 22 comments
Closed

TypeScript Typings #65

jvilk opened this issue Aug 27, 2016 · 22 comments

Comments

@jvilk
Copy link

jvilk commented Aug 27, 2016

I'm opening up this issue to start a dialog. Would you be open to generating TypeScript typings for your library, and including them in the NPM package?

Overview

I wrote a TypeScript definition generator for Stone that can automatically generate a TypeScript definition file for a JavaScript client. Stone types neatly map onto TypeScript interfaces. All the generator needs is a template to "fill in" with routes and types.

Here's the template I wrote for this SDK, which contains manual typings for the portions of your SDK that are not generated by Stone:

// Namespace that holds all of the types to avoid scope pollution.
declare module DropboxTypes {
  interface DropboxOptions {
    // An access token for making authenticated requests.
    accessToken: string;
    // The client id for your app. Used to create authentication URL.
    clientId: string;
    // Select user is only used by team endpoints. It specifies which user the team access token should be acting as.
    selectUser?: string;
  }

  class Dropbox {
    /**
     * The Dropbox SDK class.
     */
    constructor(options: { accessToken: string, clientId: string, selectUser: string });

    /**
     * Returns an instance of Dropbox that can make calls to user api endpoints on
     * behalf of the passed user id, using the team access token. Only relevant for
     * team endpoints.
     */
    actAsUser(userId: string): Dropbox;


/*ROUTES*/
  }

/*TYPES*/
}

// Global Dropbox variable, when included as script.
declare var Dropbox: typeof DropboxTypes.Dropbox;

// NPM module (require("dropbox"))
declare module "dropbox" {
  export = DropboxTypes.Dropbox;
}

(Note: There's no DropboxTeam/DropboxBase classes, since all of those JSDoc types stem from the same constructor. TypeScript has no way to disambiguate the two.)

Here's a link to the generated typings. TypeScript's type system is insufficiently expressive to support typing Promise error types, so the TSDoc for each route mentions the error type in the text.

Maintenance

  • You would have to manually maintain the TypeScript typings template when you add/remove/modify non-generated API routines, since current JSDoc-to-TypeScript systems are still somewhat rudimentary and are confused by your classes.
  • --extra-args also need to be specified using TypeScript typings and passed to the generator. In most cases, the JSDoc type aligns with the TypeScript type, so no change is needed across generators.
  • Someone would have to maintain the TypeScript definition generator as Stone evolves, or if there is a breaking change in the TypeScript language. The Stone TypeScript definition generator has already landed in a PR, though!

Benefits

I believe the benefits massively outweigh the maintenance burden, but if this is not something you are interested in, I will just upload the final definition file to DefinitelyTyped.

@braincore
Copy link
Contributor

This is definitely interesting to us! Let's land your other diff first before we address this one. Does TypeScript 2.0 have an official stable date?

@jvilk
Copy link
Author

jvilk commented Sep 6, 2016

Nope, but a release candidate is out and only a few issues remain marked for the 2.0 release. I'd guess within a month, given their track record.

@braincore
Copy link
Contributor

I've updated the SDK with the new JSDoc typing information. We can get started on this now :)

@jvilk
Copy link
Author

jvilk commented Sep 10, 2016

Once I update the generator to handle struct polymorphism, I'll open up a PR.

@jvilk
Copy link
Author

jvilk commented Sep 11, 2016

btw, I'm throwing some of the example code at the TypeScript typings to test them out, and I'm noticing that the examples omit some argument fields that are not marked as nullable in the stone typings.

For example, the auth example calls filesListFolder with {path: ''}, but omits options like recursive and include_media_info. My TypeScript typings mark all of the fields as required, as that is what the stone data types suggest.

@jvilk
Copy link
Author

jvilk commented Sep 11, 2016

Nevermind! I just noticed the default values.

@jvilk
Copy link
Author

jvilk commented Sep 11, 2016

PR now open on stone! 😄 When that's merged in, I can open a PR on dropbox-sdk-js from my fork.

@kenjiru
Copy link

kenjiru commented Oct 3, 2016

It's very nice to see that you guys are already things about supporting TypeScript typings.

When you have something in, I'm definitely going check it out!

@jvilk
Copy link
Author

jvilk commented Oct 3, 2016

@kenjiru it's completed; I'm just waiting for Dropbox to review the code. :) Feel free to check out my open PR for TypeScript definition files for stone for details.

@braincore
Copy link
Contributor

Yes, sorry, I was on PTO for 2 weeks. I'll be reviewing this soon.

@plemarquand
Copy link

@braincore @jvilk Is this still in progress? I've been holding off porting my project to the new dropbox API until it has TypeScript support.

@jvilk
Copy link
Author

jvilk commented Nov 16, 2016

@plemarquand Yeah, same here. I'm waiting on a code review. It's basically complete, pending any adjustments they ask me to make.

@kenjiru
Copy link

kenjiru commented Nov 21, 2016

@plemarquand Don't hold of, just add a declaration for the module. Starting with TypeScript 2.0, you can do this declare module "dropbox"

You lose all the typing information, so you will need to rely on unit tests. But at least you can use the new package, and you can drop the stub when the typings will become available.

@jvilk
Copy link
Author

jvilk commented Jan 22, 2017

Stone PR is merged in! I can now open a PR for this.

@jvilk
Copy link
Author

jvilk commented Jan 22, 2017

PR opened!

@denkomanceski
Copy link

Hello, is there any plan to publish the typings on the DefinitelyTyped repository? so we will be able to install them like @types/dropbox ?

@jvilk
Copy link
Author

jvilk commented Apr 11, 2017

@denkomanceski I'm not affiliated with Dropbox, but the typings should be present in the next version of the dropbox package. You will not need to install @types/dropbox, they will just be distributed with thedropbox package itself.

@pran1990
Copy link
Contributor

@denkomanceski I'll be making a release on Monday, and you should be able to grab the typings from the dropbox package itself.

@jrmgx
Copy link

jrmgx commented Apr 30, 2017

Does this has been published?
I guess I found the type file ./node_modules/dropbox/src/types.js but it is not present in the package.json so VSCode does not find type automatically (or I'm wrong somewhere)

@jvilk
Copy link
Author

jvilk commented Apr 30, 2017

No; the last published release was 2 months ago, before the TypeScript types were merged into master.

@pran1990
Copy link
Contributor

pran1990 commented Apr 30, 2017

@jrmgx published! sorry about the trouble, let me know how it works out.

@jrmgx
Copy link

jrmgx commented Apr 30, 2017

works like a charm, thx :)

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

7 participants