2016-10-07 20 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

ऐसा लगता है कि जब डॉक्स उत्पन्न किया गया, कुछ बातें चारों ओर मिलाया गया। स्पष्ट रूप से [TypeDecorator] (https://github.com/angular/angular/blob/2.0.1/modules/%40angular/core/src/util/decorators.ts#L66) से "ES7 सजावट के रूप में आमंत्रित" आया था इंटरफेस। यह थोड़े समझ में आता है कि इसके कारण, दस्तावेज़ 'प्रकार ' प्रकार के सामान्य कार्य से जुड़े थे जो 'टी' देता है, जो इसे संतुष्ट करता है। –

उत्तर

8

परिभाषा से आंकना:

export const Type = Function; 

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

Type सिर्फ एक समारोह है। Type<T> निर्मित होने पर केवल कुछ फ़ंक्शन/प्रकार है (तर्कों के किसी भी संयोजन का उपयोग करके), T बनाता है। तो दूसरे शब्दों में, एक "प्रकार" परिभाषा। याद रखें, जावास्क्रिप्ट (ओओ अर्थ में) में "प्रकार" फ़ंक्शंस का उपयोग करके दर्शाए जाते हैं। और यह कक्षाओं, इंटरफेस और टाइपस्क्रिप्ट की तरह समान है।

यह देखते हुए कि, निम्नलिखित धारण करना चाहिए:

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 
संबंधित मुद्दे