2017-05-18 18 views
1

我使用的組件react-router-bootstrap和定義從DefinitelyTyped。我的問題是下載的定義與組件不匹配。我已經創建了一個可以解決這個問題的拉取請求,但由於我不知道什麼時候會修補,所以我必須重寫它。我不能僅僅編輯位於node_modules\@types的類型定義文件,因爲我們是一個團隊在這個項目上工作,並且node_modules文件夾沒有簽入。覆蓋V2.2.2中的TypeScript類型從NPM下載@Type

如何覆蓋類型定義?由於其他文件的工作原因,我只想重寫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; 

回答

2

解決方案:

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

添加在根文件夾名爲typings文件夾。

編輯tsconfig.json:

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

添加一個文件夾中的typings -folder稱爲react-router-bootstrap(名稱應該是相同的模塊)和內部的一個名爲index.d.ts

在文件index.d.ts添加自定義typnings或引用:

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

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

當導入現在的自定義類型加載模塊:import { LinkContainer } from 'react-router-bootstrap';