<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TypeScript &#8211; blog.boro2g .co.uk</title>
	<atom:link href="https://blog.boro2g.co.uk/category/typescript/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.boro2g.co.uk</link>
	<description>Some ideas about coding, dev and all things online.</description>
	<lastBuildDate>Sat, 27 May 2017 10:45:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>
	<item>
		<title>TypeScript mixins</title>
		<link>https://blog.boro2g.co.uk/typescript-mixins/</link>
					<comments>https://blog.boro2g.co.uk/typescript-mixins/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Sat, 27 May 2017 10:43:22 +0000</pubDate>
				<category><![CDATA[TypeScript]]></category>
		<guid isPermaLink="false">https://blog.boro2g.co.uk/?p=821</guid>

					<description><![CDATA[<p>This popped into my news feed a couple days ago and it seemed like a really neat feature that&#8217;s been added into version 2.2. of TypeScript &#8211; the concept of mixins. This terminology (mixin) has been available in SASS for quite a while and provides the ability to extend functionality by composing functionality from various [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/typescript-mixins/">TypeScript mixins</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This popped into my news feed a couple days ago and it seemed like a really neat feature that&#8217;s been added into version 2.2. of TypeScript &#8211; the concept of mixins. </p>
<p>This terminology (mixin) has been available in SASS for quite a while and provides the ability to extend functionality by composing functionality from various sources. This same concept holds true in TypeScript.</p>
<p>A few examples based off a User class:</p><pre class="crayon-plain-tag">class User {
	name: string;

	constructor(name: string) {
		this.name = name;
	}
}</pre><p>Now a couple mixins:</p><pre class="crayon-plain-tag">type Constructor&lt;T&gt; = new (...args: any[]) =&gt; T;
type Constructable = Constructor&lt;{}&gt;;

function Tagged&lt;TBase extends Constructable&gt;(Base: TBase) {
	return class extends Base {
		tag: string | null;		
	};
}

function Activatable&lt;TBase extends Constructable&gt;(Base: TBase) {
	return class extends Base {
		private _isActivated = false;

		get isActivated(): boolean {
			return this._isActivated;
		}

		activate() {
			this._isActivated = true;
		}

		deactivate() {
			this._isActivated = false;
		}
	};
}</pre><p>The first adds the ability to &#8216;tag&#8217; the base class, the second exposes a couple methods to activate/deactivate the class.</p>
<p>Pulling this together you can then create basic TaggableUser:</p><pre class="crayon-plain-tag">const TaggableUser = Tagged(User);
const user = new TaggableUser(&quot;Tagged User&quot;);

console.log(user.name);
user.tag = &quot;Tagged User tag&quot;;
console.log(user.tag);</pre><p></p>
<p>Or an ActivatableUser:</p><pre class="crayon-plain-tag">const ActivatableUser = Activatable(User);
const activatableUser = new ActivatableUser(&quot;Activatable User&quot;);

console.log(activatableUser.name);
console.log(activatableUser.isActivated);
activatableUser.activate();
console.log(activatableUser.isActivated);</pre><p></p>
<p>Or even better, a combination of both:</p><pre class="crayon-plain-tag">const ActivatableAndTaggedUser = Activatable(Tagged(User));
const activatableAndTaggedUser = new ActivatableAndTaggedUser(&quot;Activatable and Tagged User&quot;);

console.log(activatableAndTaggedUser.name);
console.log(activatableAndTaggedUser.isActivated);
activatableAndTaggedUser.activate();
console.log(activatableAndTaggedUser.isActivated);</pre><p></p>
<p>Pretty neat huh?</p>
<p>The inspiration for this came from <a href="https://blog.mariusschulz.com/2017/05/26/typescript-2-2-mixin-classes" target="_blank">https://blog.mariusschulz.com/2017/05/26/typescript-2-2-mixin-classes</a></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/typescript-mixins/">TypeScript mixins</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/typescript-mixins/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
