在 Typescript 中如何忽略 NPM 包的内置声明文件?

Dav*_*aez 7 typescript

我正在使用 NPM 包react-hyperscript-helpers。当我导入它时,它会读取index.js并获取类型index.d.ts。然而,这些类型似乎已经过时了,因为我收到了很多这样的错误:

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

我知道如何修复类型错误,但我当然无法编辑下面的文件,node_modules因为它会在下一个npm install.

如何忽略包含的.d.ts文件并使用我的文件?

我尝试忽略所有内容node_modulestsconfig.json设置typings选项,但没有成功。这是该文件(如果有的话):

{
    "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"
      ]

}
Run Code Online (Sandbox Code Playgroud)

Dav*_*aez 5

第一个选项:

不要使用 @types npm 模块并将所有声明手动放入d.ts您包含在tsconfig.json. 您必须将declare module "mymod" {...}所有 pkg​​ 的定义包装起来。

除了这样做之外,请确保在您的配置中添加此编译器标志"moduleResolution": "classic"

第二个选项:

供应商您想要更正声明的整个 npm pkg。您继续使用 @types pkgs,但修复了一个不适合您的 npm pkg。

第三:

向 pkg 创建者发送 PR。

这实际上是IRC freenode中的##typescript中Arnavion帮助的结果。只是将其发布在这里供有问题的人使用。