覆盖从 NPM @Types下载的 V2.2.2 中的 TypeScript 类型



我正在使用组件react-router-bootstrap和DefinitelyTyped的定义。我的问题是下载的定义与组件不匹配。我已经创建了一个可以解决此问题的拉取请求,但由于我不知道何时会修补它,因此我必须覆盖它。我不能只编辑位于本地node_modules@types的类型定义文件,因为我们是处理该项目的团队,并且未签入node_modules文件夹。

如何重写类型定义?我只想覆盖 LinkContainer.d 文件,因为其他文件可以工作。

拉取请求:

https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16600

我尝试在我的打字文件夹中创建一个名为 LinkContainer.d.ts 的文件,该文件是正确的,但它没有被拾取。在同一个文件夹中,我有一个带有接口的global.d.ts,可以很好地拾取。

/// <reference types="react-router-bootstrap" />
import { ComponentClass } from "react";
import { NavLinkProps } from "react-router-dom";
type LinkContainer = ComponentClass<NavLinkProps>;
declare const LinkContainer: LinkContainer;
export default LinkContainer;

基于此示例的解决方案:

https://github.com/Microsoft/TypeScript/issues/11137#issuecomment-251755605

在根文件夹中添加一个名为typings的文件夹。

编辑 tsconfig.json:

{
  "compilerOptions": {
    //baseUrl and paths needed for custom typings
    "baseUrl": ".",
    "paths": {
      "*": [ "./typings/*" ]
    },
    ...

typings文件夹中添加一个名为 react-router-bootstrap 的文件夹(名称应与模块相同(,并在该文件夹中添加一个名为 index.d.ts 的文件。

在文件中index.d.ts添加自定义类型或引用:

import { ComponentClass } from "react";
import { NavLinkProps } from "react-router-dom";
type LinkContainer = ComponentClass<NavLinkProps>;
export const LinkContainer: LinkContainer;

现在导入模块时,加载了自定义类型:import { LinkContainer } from 'react-router-bootstrap';

最新更新