2016-08-05 5 views
1

タイプが割り当てられる変数のタイプについて質問があります。Typescript:タイプが割り当てられる変数を入力する

EG:

interface HTTPClient { … } 
class BrowserHTTPClient implements HTTPClient { … } 
class NodeHTTPClient implements HTTPClient { … } 

let HTTPClientType: ********; 

if (typeof window === 'undefined') { 
    HTTPClientType = NodeHTTPClient; 
} else { 
    HTTPClientType = BrowserHTTPClient; 
} 

私はタイプとして変数を入力する方法があるかどうかを把握しようとしています。私はTypescriptのドキュメントでこれを見ていません。

*******と置き換えてください、これはtypescriptでサポートされていますが、工場を作る必要はありますか?

+0

「HTTPClientType」はどのように使用しますか?それはあなたが望むタイプかクラスですか? – Alex

+0

タイプなので、多くのインスタンスを作れます。 – squirly

+0

インスタンスを作成する場合は、型ではなくクラスが必要です。 – Alex

答えて

0

1つのオプションは、コンストラクタのインタフェースを介して工場を作成することです:

export interface HTTPClientConstructor { 
    new(baseUrl: string): HTTPClient; 
} 
interface HTTPClient { … } 
class BrowserHTTPClient implements HTTPClient { … } 
class NodeHTTPClient implements HTTPClient { … } 

let HTTPClientFactory: HTTPClientConstructor; 

if (typeof window === 'undefined') { 
    HTTPClientFactory = NodeHTTPClient; 
} else { 
    HTTPClientFactory = BrowserHTTPClient; 
} 
2

私が正しくあなたの質問を理解していれば、あなたはこれを行うことができます:

let httpClientType: typeof BrowserHTTPClient | typeof NodeHTTPClient; 

if (typeof window === 'undefined') { 
    httpClientType = NodeHTTPClient; 
} else { 
    httpClientType = BrowserHTTPClient; 
} 

var httpClient = new httpClientType(); 

明確化を

class BrowserHTTPClient { } 

// myObject should be of the type BrowserHTTPClient 
// For example an instance of BrowserHTTPClient, but not necessarily as long as it's structurally compatible with the type BrowserHTTPClient 
let myObj: BrowserHTTPClient; 

// myClass should be the class BrowserHTTPClient (Or rather, it should fulfill the type of the class of the type BrowserHTTPClient, hence it can be any object that has the same structure as the _class_ BrowserHTTPClient. Inception warning here, see explanation of structural typing below.) 
let myClass: typeof BrowserHTTPClient; 

// make a instance of myClass (BrowserHTTPClient) 
myClass = BrowserHTTPClient; // Here, myClass IS the class BrowserHTTPClient. 
myObj = new myClass(); 

クラスvsタイプ

C#のような公称言語では、classtypeは事実上同じです。変数が何らかの型であると言うと、変数はその特定のクラスのインスタンス以外であっても、型がインタフェースから来たものであっても、その特定のインタフェースを実装するクラスであることはできません。

これは、構造的に型指定されたTypeScriptでは同じではなく、classtypeの概念はさらに離れています。 typescriptでクラスを定義すると、そのクラスもタイプを作成します。しかし、その型の変数を定義すると、その型を満たすものに設定できます。

class MyClass { 
    public Name: string; 
} 

class MyOtherClass { 
    public Name: string; 
    public Age: number; 
} 

let obj: MyClass; 

obj = new MyClass(); 
obj = new MyOtherClass(); // Also ok, since it fulfills the type MyClass (It has a field Name that is a string) 
+0

型がインターフェイスを実装するだけの場合はどうしますか?私が 'let HTTPClientType:typeof HTTPClient; 'と言うことができるものは無効です。 'HTTPClient'はインタフェースであるため、型ではありません。 – squirly

+0

@squirly HTTPClientは、HTTPClientインターフェイスによって作成される型ですが、クラスではないためtypeofを実行することはできません。何かがHTTPClient型になりたい場合は、その型をそのまま使用してください。 – Alex

+0

しかし、それをインスタンスの作成に使用する場合は、クラスである必要があります。そうであれば、あなたの他のクラスはそれを継承することができます。 – Alex

関連する問題