npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

es5-to-dts

v1.0.5

Published

This tool generates TypeScript declaration files from legacy JS code. It works by identifying certain patterns from old JS.

Downloads

17

Readme

ES5 To DTS

This tool generates TypeScript declaration files from legacy JS code. It works by identifying certain patterns from old JS.

It might not work properly or cover 100% of the possible cases, so you'll probably need to tweak the generated dts manually. But it gets the job done and save a lot of time. It is even better if the legacy code contains JsDoc comments.

Made using TypeScript compiler API and this AST visualizer:
https://astexplorer.net/

Caveats

As I said, the tool may not be able to handle some cases. Errors caught during generation are archived and can be viewed at the end:
Error handling

Feel free to open an issue with the logs and the concerned part of code.

Changelog

  • 1.0.0: Initial release
  • 1.0.3: Fixed lib.es5.d.ts not found
  • 1.0.4: Added two more patterns to handle
  • 1.0.5:
    Restructured code
    Added error handling Added unit testing Added many more patterns to handle

Installation

npm -g i es5-to-dts
To generate a declaration file:
es5-to-dts oldFile.js NamespaceName

Example

Old JS code:


Number.prototype.mod = function(n) {
  return ((this % n) + n) % n;
};

SomethingGlobal.changed = true;
var isNotGlobal;
isNotGlobal.a = 5;

function Animal(name, age, race) {
  this.name = name;
  this.age = age;
  this.race = race;
  this._owner = null;
}

Object.defineProperty(Animal.prototype, 'owner', {
  get: function() {
      return this._owner;
  },
  configurable: true
});

/**
 * Create a new dog
 *
 * @param {String} name
 * @param {'Pug'|'Shiba Inu'} race
 */
function Dog() {
  this.initialize.apply(this, arguments);
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog._count = 0;

Dog.prototype.initialize = function(name, race) {
  Animal.prototype.initialize.call(this, name, 0, race);
  this.likes = 'bones';
  this.friend = new Animal();
  Dog._count++;
};

/**
 * Bark for a number of times
 *
 * @param {Number} times
 */
Dog.prototype.bark = function(times) {
  this.barking = true;
};

Dog.getCount = function() {
  return this._count;
}

Result:

declare global {
    interface Number {
        mod: (n: any) => number;
    }
    interface SomethingGlobal {
        changed: boolean;
    }
}

export declare namespace MyNamespace {
    class Animal {
        new(name: any, age: any, race: any);
        name: any;

        age: any;

        race: any;

        _owner: any;

        readonly owner: any;
    }
    
    class Dog extends Animal {
        /**
         * Create a new dog
         *
         * @param {String} name
         * @param {'Pug'|'Shiba Inu'} race
         */
        new(name: string, race: 'Pug' | 'Shiba Inu');
        static _count: number;

        likes: string;

        friend: Animal;

        /**
         * Bark for a number of times
         *
         * @param {Number} times
         */
        bark: (times: number) => any;

        barking: boolean;

        static getCount: () => any;
    }
}