覆盖从 NPM @Types 下载的 V2.2.2 中的 TypeScript 类型 [英] Override TypeScript types in V2.2.2 downloaded from NPM @Types

查看:29
本文介绍了覆盖从 NPM @Types 下载的 V2.2.2 中的 TypeScript 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用组件 react-router-bootstrap 和定义来自 DefinitelyTyped.我的问题是下载的定义与组件不匹配.我创建了一个 pull request 来解决这个问题,但由于我不知道它什么时候会被修补,所以我必须覆盖它.我不能只在本地编辑位于 node_modules\@types 中的类型定义文件,因为我们是一个致力于此项目的团队,并且没有签入 node_modules 文件夹.

I'm using the component react-router-bootstrap and definitions from DefinitelyTyped. My problem is that the definitions downloaded does not match the component. I have created a pull request that will solve this but since I do not know when it will be patched I have to override it. I cannot just edit the type definitions file located in node_modules\@types locally because we are a team working on this project and the node_modules folder is not checked in.

如何覆盖类型定义?我只是不想覆盖 LinkContainer.d 文件,因为其他文件有效.

How can I override type definitions? I only wan't to override LinkContainer.d file since the others files work.

拉取请求:

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

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

I tried to create a file named LinkContainer.d.ts in my typings folder that was correct but it does not get picked up. In the same folder I have a global.d.ts with interfaces that gets picked up fine.

/// <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;

推荐答案

基于此示例的解决方案:

Solution based on this example:

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

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

Add a folder called typings in root folder.

编辑 tsconfig.json:

Edit tsconfig.json:

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

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

Add a folder in the typings-folder called react-router-bootstrap (name should be identical to module) and inside that a file called index.d.ts.

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

In the file index.d.ts add your custom typnings or references:

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

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

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

When importing the module now the custom types were loaded: import { LinkContainer } from 'react-router-bootstrap';

这篇关于覆盖从 NPM @Types 下载的 V2.2.2 中的 TypeScript 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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