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

typewiz

v1.2.4

Published

Automatically discover and add missing types in your TypeScript code

Downloads

4,757

Readme

TypeWiz

Automatically discover and add missing types in your TypeScript code.

Build Status Coverage Status code style: prettier

Introduction

TypeWiz monitors your variable types in runtime, and uses this information to add missing type annotations to your TypeScript code. For instance, given the following source code as input:

function add(a, b) {
    return a + b;
}
add(5, 6);

TypeWiz will automatically detect the types of a and b as number, and will rewrite to code to read:

function add(a: number, b: number) {
    return a + b;
}
add(5, 6);

You can learn more about the project in the blog posts:

Installation

This package provides a Command Line Interface (CLI) for working working with TypeWiz. If you are looking to use TypeWiz with WebPack, check out the TypeWiz WebPack Plugin. For node.js integration please check out the typewiz-node runner.

Install the TypeWiz CLI by running the following command:

yarn global add typewiz --ignore-engines

or

npm install -g typewiz

Usage

Calculate Your Type Coverage

Use the coverage command and point it at your project's tsconfig.json to calculate and display the type coverage:

typewiz coverage /path/to/projec/tsconfig.json

The output looks similar to this:

12236 of 13298 types are known.
Your type coverage is: 92.01%

The percentage represent the amount of identifier in the code whose type is known - i.e. that have a specific type (or such type could be inferred by TypeScript) which is not any. The higher the better. You can improve this number by adding more types to your code, for instance by using TypeWiz. This metric allows you to measure your progress in adding new types to your TypeScript project.

Instrument Your Code

TypeWiz works by instrumenting your code and adding special code that tracks the type you pass to functions on run time. In most cases, the you will use one of the existing integrations that will take care of instrumenting for you.

In some scenarios, for example, when an integration with your tool chain does not exist yet, you will want to instrument your code manually using the instrument command:

typewiz instrument -p myFile.ts

The above command will instrument myFile.ts and write the result in place, overwriting the original input file. You can remove the -p flag to output the instrumented source code to the standard output, or change it to -o filename.ts to write the output to a different file.

Apply discovered types

After you ran your instrumented code, you will have to collect the discovered types and save them to a file so you can add them to your code using the applyTypes command. This is not required when you use the Node.js integration, as it already adds the types for you. When you use other integrations, such as the Webpack plugin, the discovered types will be written to a file on disk, and you can use the CLI to add them to your code:

typewiz applyTypes collected-types.json

You can also specify a prefix that will be prepended to all the types added to your code, to distinguish them from the existing types. For instance:

typewiz applyTypes -p /*typewiz*/ collected-types.json

will result in /*typewiz*/ appearing before each discovered type, e.g.:

function add(a: /*typewiz*/number, b: /*typewiz*/number) {
    return a + b;
}
add(5, 6);

This can also be useful for quickly switching the discovered types on/off, a technique discusssed in the blog post.

Specifying configuration file

You can specify a TypeWiz configuration file using the -c argument, for instance:

typewiz -c my-typewiz-conf.json instrument myScript.ts

This can be useful to override some defaults - see the Configuration Options section for additiona details. If not configuration file is specified, typewiz will try to read the configuration from a file called typewiz.json if such file exists in the current directory or any of its ancestors.

License

Copyright (C) 2018, Uri Shaked. Licensed under the MIT license.