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

ignore-bad-ts

v1.0.4

Published

Comands to modify files of a typescript dependency that was written in malformed typescript

Downloads

3

Readme

ignore-bad-ts

This project provides various CLI and programmatic APIs to avoid type-checking failures in external libraries that you have no control over. This is meant to enable you to keep stricter type safety in your project without being unable to use typescript as your main compiler.

Installation

    yarn add --dev ignore-bad-ts

How this could happen:

Perhaps you've found yourself in this situation. You love typescript and all the security and scalability that it provides.

However, you're in an organization that is only now learning to love typescript like you. And they're learning at a code-fast, type-later pace.

They ask you to do this:

import CompanyModule from 'my-company-module';

// All your awesome typed code here

And when you run it, you're seeing errors from... your node_modules!

node_modules/my-company-module/src/it-has-ts-at-least.ts - error TS7053 Element implicitly has an 'any' type...

Admittedly, the above error isn't THAT critical of a rule, but you still can't compile without changing your tsconfig. So what happened?

Well...

  • maybe your co-workers decided to turn off particular best-practice settings and/or only provide their code as untranspiled es6.
  • maybe your co-workers liked the idea of typescript in IDE for intellisense but have been transpiling with babel instead
  • maybe there is literally no declaration files or broken declaration files due to bad CI/CD

So what do you do?

You could just disable all typescript compilation and use something like babel to transpile without the typechecking.

Hark, my friend! Do no let your code quality and type-checking slide so that your own codebase can be broken later on too!

Instead you have ignore-bad-ts!

What does it do?

In short, the scripts in this package will add "@ts-nocheck" at the top of all .ts(x) files that match the globs you provide. This solution may become deprecated if per module type checking is added to the typescript compiler itself.

Usage

CLI

Once installed, you have the option of running a CLI tool

ignore-bad-ts node_modules/my-company-module/**

In the above example, we are telling ignore-bad-ts to inject ts-ignore tags on all current files in the directory that match the glob "node_modules/my-company-module/**".

Programmatic API

This package also provides programmatic methods for marking files as ignored programmatically.

ignoreFiles

This method is an async version that will find and modify all matching files.

ignoreFilesSync

This method will find and modify all matching files synchronously. This is not recommended, but is provided for when you are restricted to synchronous execution (like nextjs configs).

Arguments

Both functions generally match the argument signature of sync/stream from fast-glob.

There are a few differences:

  1. Output options are not allowed from fast-glob (This would affect how the glob output is processed)
  2. debug-glob - Use this to log all files that matched the globs. (Helpful for verifying your glob is doing what you think)
  3. debug-ts - Use this to log all files that matched for writing @ts-nocheck. (Helpful for verifying only the ts files that should be written to)
  4. dry-run - Use this to run the current command and view the logs but to not actually alter the files. (Helpful for quick debugging)

Recipes/Examples

NextJS config

If using NextJS, the strategy would be to synchronously call the ignore on your designated modules as your config file is instantiated.

// next.config.js
const { ignoreFilesSync } = require('ignore-bad-ts');

// Apply the ignore as we initialize this module, so nextJS can compile, etc.
ignoreFilesSync(['node_modules/my-company-module/**']);


module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    // Important: return the modified config
    return config
  },
}

yarn command pre-command

You may want to simply add the cli command in scripts to ensure they build correctly.

Keep in mind, that you would need to apply this at every command that uses typechecking, since any new developer may run one command out of order with the other and fail to get a compilation.

In the below example, we assume that jest is using ts-jest, and so we also want to ignore-bad-ts when calling it.

// package.json
{
    ...,
    "scripts": {
        "build": "ignore-bad-ts node_modules/my-company-module/** && tsc",
        "test": "ignore-bad-ts node_modules/my-company-module/** && jest"
    },
    ...
}

Monorepo

Assuming that you've used something like lerna to setup a monorepo with packages/{package_name}, you may want to change the glob to point to the upper directory where node_modules is consolidated.

// packageA package.json
{
    ...,
    "scripts": {
        "build": "ignore-bad-ts ../../node_modules/my-company-module/** && tsc",
        "test": "ignore-bad-ts ../../node_modules/my-company-module/** && jest"
    },
    ...
}

Monorepo Programmatic Implementation

Assuming we have a package that is also a NextJS site, we can take advantage of the cwd command to set the directory we check for globs from.

// packageA next.config.js
const { ignoreFilesSync } = require('ignore-bad-ts');
const path = require('path');

// Apply the ignore as we initialize this module, so nextJS can compile, etc.
ignoreFilesSync(['node_modules/my-company-module/**'], { cwd: path.resolve(__dirname, '../..' });


module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    // Important: return the modified config
    return config
  },
}

Development Notes

If you would like to test a feature or changes to this package, I highly recommend setting up a local verdaccio instance.

TBD: I will link a bare bones local verdaccio registry for running on your local machine. This allows you to separately import your package. :D