Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override incorrect types from a package that ships its own .d.ts?

I am using chalk with a JavaScript project that I'm checking with TypeScript's checkJs flag.

The JavaScript code imports it like so:

const chalk = require('chalk')

Unfortunately, chalk ships its own types, and its types are wrong. It should use the syntax export = chalk (which is TypeScript's syntax for CommonJS) but instead does export default chalk.

Somebody submitted a PR to fix it, but the project hasn't merged any PRs in a while.

How can I work around the bad export from the chalk typings?

like image 898
Nathan Shively-Sanders Avatar asked Nov 05 '25 19:11

Nathan Shively-Sanders


1 Answers

You can override the package's types by providing a path mapping:

  1. Create a file named chalk-override.d.ts in your project root. Copy the chalk types in there and fix them.
  2. Add the following to your tsconfig.json (create one with tsc --init --checkJs if you haven't already):

    {
        // ... other settings ...
    
        "baseUrl": "./",
        "paths": {
            "chalk": ["chalk-override"]
        }
    }
    
like image 52
Nathan Shively-Sanders Avatar answered Nov 09 '25 07:11

Nathan Shively-Sanders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!