2017-02-10 120 views
0

我想援引打字稿以下方法中的所有功能和接口:導入自定義類型

nlp.text("Hi Dr. Miller the price is 4.59 for the U.C.L.A. Ph.Ds.").sentences.length 
// 1 

什麼會正確的import語句是讓我從這個types definition做到這一點?

請注意,text()是此定義中函數和各種方法的名稱。前者需要參數,而方法則不需要。我對這個功能很感興趣。

我只是不確定哪個符號會給我在js文件here中定義的NLP函數。

回答

1
import * as nlp from 'library' 
// import { text } from 'library' - this would be much better 

const length = nlp.text("...").sentences.length 
// const length2 = text("...").sentences.length - using the second type of import 

很明顯,您將不得不用'library'替換您導入的包的名稱。

如果你想給導入的物體的類型,以及地方使用它,你可以做這樣的事情(這是不是一個真正的好主意,雖然保持類型定義在2處):

interface Nlp { 
    text(string: string, options?: any): Text 
    sentence(string: string): Sentence 
    // ... 
} 

function iTakeAnNlp(input: Nlp): void { 
    // ... 
} 

iTakeAnNlp(nlp) 
+0

我該如何創建一個類型爲'nlp'的類屬性? –

+0

你爲什麼要這麼做?爲了完成你在問題中所描述的這個完美的作品。 –

+0

這是一個很好的問題。我想我已經習慣了創建這種屬性的模式,然後在我班的方法中調用它們。例如'someMethod(){let foo = this.nlp.text('String')。sentences.length}' –