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

IT技术 reactjs typescript react-bootstrap
2021-04-27 11:14:11

我正在使用来自absoluteTyped的组件react-router-bootstrap和定义我的问题是下载的定义与组件不匹配。我创建了一个 pull request 来解决这个问题,但由于我不知道它何时会被修补,所以我必须覆盖它。我不能只编辑位于本地的类型定义文件,因为我们是一个致力于此项目的团队,并且未签入文件夹。node_modules\@typesnode_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;
1个回答

基于此示例的解决方案:

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(名称应与module相同),并在其中添加一个名为index.d.ts.

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

import { ComponentClass } from "react";
import { NavLinkProps } from "react-router-dom";

type LinkContainer = ComponentClass<NavLinkProps>;
export const LinkContainer: LinkContainer;

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