简体   繁体   中英

In Typescript how to Ignore NPM package's built-in declaration file?

I'm using NPM package react-hyperscript-helpers . When I import it it'll read index.js and take the types from index.d.ts . These types however seem to be outdated since I get a lot of errors like this:

node_modules/react-hyperscript-helpers/lib/index.d.ts(546,53): error TS2314: Generic type 'HTMLAttributes<T>' requires 1 type argument(s).

I know how to fix the type errors but I cannot of course edit the file under node_modules because it would get overwritten in the next npm install .

How do I ignore the included .d.ts file and use mine instead?

I tried ignoring all of node_modules in tsconfig.json and setting the typings option but without any success. Here's that file if it's any use:

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es5",
        "jsx": "react",
        "typeRoots" : ["./typings"]
    },
    "include": [
          "typescript/**/*"
      ],
      "exclude": [
        "node_modules/**/*",
        "node_modules/react-hyperscript-helpers/lib/*.ts"
      ]

}

First option:

Don't use @types npm modules and put all the declarations by hand in d.ts files that you include in your tsconfig.json . You'll have to wrap the definition with declare module "mymod" {...} for all pkgs.

Besides doing this make sure to add this compilerFlag in your config "moduleResolution": "classic"

Second option:

Vendor the whole npm pkg you want to correct the declarations for. You keep using @types pkgs but fix the one npm pkg not working for you.

Third:

Send a PR to the pkg creator.

This is actually the result of help of Arnavion in ##typescript in IRC freenode. Just posting it here for people with the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM