2017-10-15 80 views
1

只要看看這個打字稿代碼:TypeScript。如何使用未導出的類型定義?


lib.ts

interface Human { 
    name: string; 
    age: number; 
} 

export default class HumanFactory { 
    getHuman(): Human { 
     return { 
      name: "John", 
      age: 22, 
     } 
    } 
} 

index.ts

import HumanFactory from "./lib"; 

export class Foo { 
    human: any; 

    constructor() { 
     const factory = new HumanFactory(); 
     this.human = factory.getHuman(); 
    } 

    diffWithError(age: number): number { 
     return age - this.human.name; 
    } 

    diffWithTypingAndAutocoplete(age: number): number { 
     const factory = new HumanFactory(); 
     return age - factory.getHuman().name; 
    } 
} 

在 「富」 的 「人性化」 屬性的問題類。我無法將此變量的類型定義爲來自lib.ts的「Human」接口。

在方法「diffWithError」我做了錯誤 - 在算術運算使用號碼「年齡」和字符串「名」,但既不IDE也不TS編譯器知道這一點,因爲在這種情況下,鍵入「this.human的。名稱「是」any「

在」diffWithTypingAndAutocoplete「方法中,我只使用方法」getHuman「。 IDE和編譯器知道方法結果的類型。這是「人」界面,字段「名稱」是「字符串」。此方法在編譯源時會觸發錯誤。


我發現這個問題,當我試圖導入JS庫的.d.ts文件,我沒有能力導出所需的接口。無論何時我想定義類型(並且沒有內聯類型定義,如{name:string,age:number}),我是否可以以某種方式定義有效類型的「人類」屬性,而無需複製和粘貼「Human」接口的代碼。

我不想創建未導出的類的實例,我只想要類型檢查和自動完成。


P.S.我試着寫:

human: Human 

編譯器觸發的錯誤:「錯誤TS2304:找不到名稱‘人’」(預期的行爲)


PSS我嘗試用三重斜線指令,要做到這一點:

///<reference path="./lib.ts" /> 

但是這不起作用。


對不起,我的英語不好,感謝您的回答

回答

0

如果你不能改變lib.ts可以「查詢」返回getHuman函數的類型。

import HumanFactory from "./lib"; 

const dummyHuman = !true && new HumanFactory().getHuman(); 
type Human = typeof dummyHuman; 

export class Foo { 
    human: Human; 

    // ... 
} 

!true &&是用來防止new HumanFactory().getHuman()執行:因爲目前的打字原稿沒有提供這方面的任何直接的方法,這是一個有點棘手。

+0

不幸的是,它不起作用:錯誤TS2322:類型'Human'不能分配給'false'類型。當我在構造函數 –

+0

中嘗試這個'this.human = factory.getHuman()'你使用哪種版本的打字稿? –

1

需要導出Human以便它是可見的 - 和使用 - 從index.ts以及(如HumanFactory)。不要使用默認的出口,但「命名出口」,即嘗試這種

export interface Human { 
    name: string; 
    age: number; 
} 

export class HumanFactory { 
    getHuman(): Human { 
     return { 
      name: "John", 
      age: 22, 
     } 
    } 
} 

index.ts

import { Human, HumanFactory} from "./lib"; 

**編輯**

如果你不能改變lib.d.ts然後重新Human並使用雙鑄造即

import HumanFactory from "./lib"; 

interface Human { 
    name: string; 
    age: number; 
} 

export class Foo { 
    human: Human; // <= change here 

    constructor() { 
     const factory = new HumanFactory(); 
     this.human = factory.getHuman() as any as Human; // <= double casting 
    } 

    diffWithError(age: number): number { 
     return age - this.human.name; 
    } 

    diffWithTypingAndAutocoplete(age: number): number { 
     const factory = new HumanFactory(); 
     return age - factory.getHuman().name; 
    } 
} 
+0

我無法編輯'lib.ts'。在我的項目中,這是在.d.ts文件中外部庫的類型定義。 –

+0

@NazikOrl看到我的編輯替代解決方案 –

+0

感謝您的答案。我發現[另一個解決方案](https://stackoverflow.com/a/46774999/8779223)對我來說可接受 –

0

我找到了解決方案!

我把文件人類interface.ts與此內容:主文件

import HumanFactory from './lib'; 

const humanObject = new HumanFactory().getHuman(); 
type HumanType = typeof humanObject; 

export default interface Human extends HumanType {} 

導入這個接口的不執行「HumanFactory」的創作和類型檢查正常工作。

感謝您的想法typeof