【问题标题】:Override TypeScript types in V2.2.2 downloaded from NPM @Types覆盖从 NPM @Types 下载的 V2.2.2 中的 TypeScript 类型
【发布时间】:2017-05-18 19:51:18
【问题描述】:

我正在使用组件 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;

【问题讨论】:

    标签: reactjs typescript react-bootstrap


    【解决方案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 的文件夹(名称应与模块相同),并在其中添加一个名为 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';

    【讨论】:

      猜你喜欢
      • 2016-09-29
      • 2018-05-18
      • 2017-03-12
      • 2016-12-29
      • 2019-03-02
      • 2021-01-27
      • 2017-05-28
      • 2019-02-22
      • 2018-07-11
      相关资源
      最近更新 更多