Typescript Manual Translation Series 8-common errors and Mixins

Source: Internet
Author: User

Common Errors

The following lists some common errors that are frequently encountered during the use of the typescript language and compilers .

"Tsc.exe" exited with error code 1.

Solution: Check the file encoding to UTF-8- https://typescript.codeplex.com/workitem/1587

External Module XYZ cannot be resolved

Solution: Check that the module path is case sensitive - https://typescript.codeplex.com/workitem/2134

mixins

Along with traditional OO hierarchies, another popular-in-the-building-classes from reusable-is-to-build the m by combining simpler partial classes. Familiar with the idea of mixins or traits for languages like Scala, and the pattern have also reached some popu larity in the JavaScript community.

Mixin Sample

In the code below, we show how can model Mixins in TypeScript. After the code, we'll break off how it works.

Disposable Mixin
Class Disposable {
Isdisposed:boolean;
Dispose () {
This.isdisposed = true;
}

}

Activatable Mixin
Class Activatable {
Isactive:boolean;
Activate () {
This.isactive = true;
}
Deactivate () {
This.isactive = false;
}
}

Class SmartObject implements disposable, Activatable {
Constructor () {
SetInterval (() = Console.log (this.isactive + ":" + this.isdisposed), 500);
}

Interact () {
This.activate ();
}

Disposable
Isdisposed:boolean = false;
Dispose: () = void;
Activatable
Isactive:boolean = false;
Activate: () = void;
Deactivate: () = void;
}
Applymixins (SmartObject, [disposable, Activatable])

var smartobj = new SmartObject ();
SetTimeout (() = Smartobj.interact (), 1000);

////////////////////////////////////////
In your runtime library somewhere
////////////////////////////////////////

function Applymixins (Derivedctor:any, basectors:any[]) {
Basectors.foreach (Basector = {
Object.getownpropertynames (Basector.prototype). ForEach (name = = {
Derivedctor.prototype[name] = Basector.prototype[name];
})
});
}

Understanding the sample

the code sample starts with the both classes that would act is our mixins. You can see each one are focused on a particular activity or capability. We ll later mix these together to form a new class from both capabilities.

Disposable Mixin
Class Disposable {
Isdisposed:boolean;
Dispose () {
This.isdisposed = true;
}

}

Activatable Mixin
Class Activatable {
Isactive:boolean;
Activate () {
This.isactive = true;
}
Deactivate () {
This.isactive = false;
}
}


Next, we'll create the class that would handle the combination of the mixins. Let's look at the it does this: detail

Class SmartObject implements disposable, Activatable {


the first thing notice in the above are that instead of using ' extends ', we use ' implements '. This treats the classes as interfaces, and only uses the types behind disposable and Activatable rather than the implement ation. This means that we'll have to provide the implementation in class. Except, that's exactly what do we want to avoid by using mixins. 

to satisfy this requirement, we create stand-in properties and their types for the members that would come from our mixins . This satisfies the compiler, these members would be available at runtime. This lets us still get the benefit of the mixins, albeit with a some bookkeeping overhead.

Disposable
Isdisposed:boolean = false;
Dispose: () = void;
Activatable
Isactive:boolean = false;
Activate: () = void;
Deactivate: () = void;


Finally, we mix our mixins to the class, creating the full implementation.

Applymixins (SmartObject, [disposable, Activatable])


Lastly, we create a helper function that would do the mixing for us. This would run through the properties of each of the mixins and copy them the The mixins, filling out the Stand-in properties with their implementations.

function Applymixins (Derivedctor:any, basectors:any[]) {
Basectors.foreach (Basector = {
Object.getownpropertynames (Basector.prototype). ForEach (name = = {
Derivedctor.prototype[name] = Basector.prototype[name];
})
});
}

References

[1] Http://www.typescriptlang.org/Handbook#common-errors

[2] typescript Series 1-Introduction and version new features, http://my.oschina.net/1pei/blog/493012

[3] typescript series 2-manuals-basic type, http://my.oschina.net/1pei/blog/493181

[4] typescript series 3-manuals-interfaces, http://my.oschina.net/1pei/blog/493388

[5] typescript series 4-Manuals-class, http://my.oschina.net/1pei/blog/493539

[6] typescript Series 5-manuals-modules, http://my.oschina.net/1pei/blog/495948

[7] typescript series 6-manuals-functions, http://my.oschina.net/1pei/blog/501273

[8] typescript manual translation Series 7-generics, http://my.oschina.net/1pei/blog/513483

Typescript Manual Translation Series 8-common errors and Mixins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.