2016-10-07 3 views
6

ドキュメントの多くの場所でTypeというキーワードが表示されています。例えば、as seen hereComponentRefは、componentTypeプロパティを持っています。それはタイプType<any>であると言われています。さらに検索すると、ドキュメント上でthis entryが見つかりました。それは言う:ES7デコレータとして呼び出す角度2のタイプとは何ですか?

はまた、githubの上up the sourceをご覧の上、私はこれらのコメントを見つける:

/** 
* @whatItDoes Represents a type that a Component or other object is instances of. 
* 
* @description 
* 
* An example of a `Type` is `MyCustomComponent` class, which in JavaScript is be represented by 
* the `MyCustomComponent` constructor function. 

は、しかし、私は Typeが何にとして、まだ明確ではないです。何か基本的なものが欠けていますか?定義から判断

+1

ドキュメントが生成されたとき、いくつかのものは、周りの混合得たように見えます。 "Invoke as ES7 decorator"は、[TypeDecorator](https://github.com/angular/angular/blob/2.0.1/modules/%40angular/core/src/util/decorators.ts#L66)から来たようです。インタフェース。それはそれが原因であることがちょっとわかりました。ドキュメントは 'Type 'のジェネリック関数に関連付けられていて、 'T'を返します。 –

答えて

8

export const Type = Function; 

export interface Type<T> extends Function { 
    new (...args: any[]): T; 
} 

Typeは単なる関数です。 Type<T>は、(引数の任意の組み合わせを使用して)構築されたときにちょうど関数/タイプであり、Tを作成します。つまり、「型」の定義です。覚えておきますが、javascript(オブジェクト指向の意味で)の「型」は関数を使って表されます。そしてそれは、種類、インタフェースなどをタイプコピーに置き換えたものです。

は以下が保持すべきである、ということを考える:

class Foo { 
    s: string; 
} 
class Bar { 
    s: number; 
} 
class Biz { 
    ss: string; 
} 
class Baz { 
    s: string; 
    t: number; 
} 

let x: Type<{ s: string }>; // x is a type that returns an object 
          // with an s property of type string 

x = Foo; // ok 
x = Bar; // error, s is not a string 
x = Biz; // error, doesn't contain s property 
x = Baz; // ok 
x = { s: "foo" }; // haha nice try 
関連する問題