Exclude/overwrite npm-provided typings

前端 未结 2 917
半阙折子戏
半阙折子戏 2020-11-30 04:45

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

2条回答
  •  粉色の甜心
    2020-11-30 05:16

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

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

    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. place a folder named like the module inside the declarations folder, containing a file called index.d.ts for the typings

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

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

    An important hint by 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

提交回复
热议问题