2017-04-01 37 views
0

我想從外部類型擴展我自己的類型之一。手稿定義文件和擴展外部類型

因爲我真的不知道我在做什麼,我把這些放在global.d.ts文件中。

在此之前,我一直在導入類型到我的項目中的每個打字稿文件中,這似乎是錯誤和混亂的(每個文件頂部有30個導入行)。

global.d.ts文件時,我只是有我的代碼在它

// successfully can use this anywhere 
interface IPerson { 
    name: string 
} 

如果我的理解是停止什麼,如果我有需要延長一個對外一個類型。

(例如,我將嘗試擴展Redux動作類型)。

/// <reference path="../node_modules/redux/index.d.ts" /> 

interface IPerson { 
    name: string 
} 

// even with the reference typescript 'cannot find name Action' 
interface PersonAction extends Action { 
    person?: IPerson 
} 

我能順利拿到打字稿停止抱怨,如果我更改文件這一

import * as Redux from 'redux'; 

interface IPerson { 
    name: string 
} 

interface PersonAction extends Redux.Action { 
    person?: IPerson 
} 

除了現在每一個使用IPerson再也找不到文件,它不能找到的名字行動「它。我不明白爲什麼。任何時候,我將一個導入添加到聲明文件中,它會破壞其他所有內容,但會修復聲明文件。

我在這裏做錯了什麼?我正在尋找一個我可以放置所有類型的地方,如果有更好的方式讓打字稿識別我的類型,我可以選擇其他選項。

回答

0

我認爲你需要從你的模塊export接口:

import {IPerson, PersonAction} from 'your-module' 

附註:

import * as Redux from 'redux'; 

export interface IPerson { 
    name: string 
} 

export interface PersonAction extends Redux.Action { 
    person?: IPerson 
} 

,然後import在使用地點相同雖然這不是問題,但您可能需要loose the 'I' from the name of interfaces