What is angular 2 type?

I met the Type keyword in many places in the documentation. For example, as shown here, ComponentRef has the componentType property. They say that it is of type Type<any> . Upon further search, I find this entry about this in the docs. It says: "Call as an ES7 decorator."

Also when searching up the source in github, I find these comments:

 /** * @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. 

However, I still don't understand what Type does. Did I miss something?

+6
source share
1 answer

Judging by definition:

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

Type is just a function. Type<T> is just some function / type when building (using any combination of arguments), creates T That is, in other words, a type definition. Remember that "types" in javascript (in the sense of OO) are represented using functions. And it corresponds to classes, interfaces, etc. In typescript.

Given that the following should be done:

 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 
+8
source

All Articles