排除/覆盖 npm 提供的类型 [英] Exclude/overwrite npm-provided typings

查看:27
本文介绍了排除/覆盖 npm 提供的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个写得不好、过时的 npm 包.我已经编写了自己的类型,现在我想知道是否可以从 npm 包中排除原始类型.不是简单的接口扩展,原来在这点上基本都是垃圾.

I've got an npm package with badly written, out of date typings. I've written my own typings and now I'm wondering if I can somehow exclude the original typings from the npm package. It's not a simple extension of interfaces, the originals are basically garbage at this point.

在 tsconfig.json 中使用排除列表当然不适用于此目的,因为即使您排除该文件夹,它仍会从 node_modules 加载文件.

Using the exclude list in tsconfig.json does not work for this purpose of course, since it still loads files from node_modules even if you exclude that folder.

推荐答案

您可以使用 tsConfig 中的路径选项获得所需的行为它可能看起来像这样:

You can get the desired behavior with the paths option in the tsConfig It could look something like this:

{
    "compilerOptions": {
       ...
        "paths": {
            "*": [
                "src/*",
                "declarations/*"
            ]
        }
    },
    ...
}

使用这个配置打字稿在 src(应该有所有应用程序源)和声明中查找模块,在声明文件夹中,我通常放置我额外需要的声明.

With this config typescript looks for modules in src (there should be all the app source) and also in declarations, in the declarations folder I usually place my extra needed declarations.

要覆盖节点模块的类型,有两个选项:

To override the typings of a node module there are two options:

  1. 在声明文件夹中放置一个名为模块的文件夹,其中包含一个名为 index.d.ts 的文件,用于输入

  1. place a folder named like the module inside the declarations folder, containing a file called index.d.ts for the typings

将一个声明文件,命名为模块,放在声明文件夹中

place a declaration file, named like the module, inside the declarations folder

作为一个工作示例,你可以看看这个 repo https://github.com/kaoDev/react-ts-sample

As a working example you can take a look at this repo https://github.com/kaoDev/react-ts-sample

Bernhard Koenig 的重要提示:

路径的顺序很重要.我必须将带有我的覆盖的路径放在具有原始类型定义的路径之前,以便我的覆盖首先被拾取.– 伯恩哈德·柯尼格

The order of the paths matters. I had to put the path with my overrides before the path with the original type definitions so my overrides get picked up first. – Bernhard Koenig

这篇关于排除/覆盖 npm 提供的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆