2016-10-03 19 views
0

我在VSCode中使用TypeScript 2.0,但是,突出顯示的錯誤全部由TypeScript編譯器確認。所以我導入模塊:只有在沒有對其屬性的引用時才能找到Typescript類?

import * as els from 'elasticsearch'; 

其中elasticsearch已安裝的定義,例如npm i @types/elasticsearch -S

現在,如果在我的課我有一個ELS型像這樣的屬性:

private _client: els.Client; 

,沒有問題,但是如果我有一類這樣的特性:

search(term: string): Promise<els.Client.search> {} 

然後我得到的錯誤:

Module 'Elasticsearch' has no exported member 'Client'

enter image description here

如果我正在尋找它的一個屬性,但是如果我只是尋找它,不能找到這個類怎麼能?

回答

0

你是對的,錯誤信息令人困惑。它起源於您嘗試使用els.Client.search作爲類型。如果你試試這個你得到類似的消息:

import * as els from 'elasticsearch'; 

class Foo { 
    private _client: els.Client; 

    y: els.Client.search; 

    bar() {} 

    x: Foo.bar; 
} 

error TS2305: Module 'Elasticsearch' has no exported member 'Client'.

error TS2503: Cannot find namespace 'Foo'.

注意如何在第二消息中也抱怨說,它不能在Foo類中正確找到Foo。你可能會考慮發佈關於這個打字稿的問題。

How can the class not be found if I'm looking for one of its properties, but not if I just look for it?

真正的問題是,你可能希望你的search的返回類型是一樣的els.Client.search返回類型。我認爲除了本質上覆制els.Client.search聲明外,沒有更好的方法可以做:

search<T>(term: string): Promise<els.SearchResponse<T>> {} 
相關問題