<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhishek on Medium]]></title>
        <description><![CDATA[Stories by Abhishek on Medium]]></description>
        <link>https://medium.com/@h6165?source=rss-366e4359ccc1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hu5iV3bED63F8IwP.jpeg</url>
            <title>Stories by Abhishek on Medium</title>
            <link>https://medium.com/@h6165?source=rss-366e4359ccc1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:07:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@h6165/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Starting Typescript]]></title>
            <link>https://medium.com/@h6165/starting-typescript-ff973d3311cc?source=rss-366e4359ccc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff973d3311cc</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Abhishek]]></dc:creator>
            <pubDate>Wed, 06 Dec 2017 13:11:02 GMT</pubDate>
            <atom:updated>2017-12-06T13:11:02.245Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently started using Typescript and was pleasantly surprised. The experience was productive and enjoyable, so much so that I’m considering including Typescript on all my projects now.</p><p>Typescript is a super-set of Javascript that adds type annotations. As someone with majority experience in dynamic languages (Ruby and Javascript), the first hurdle to trying Typescript is the prejudice. Types bring bad memories of programming in Java or C++. Not only is it verbose, it imposes an upfront cognitive load — we need to think about data-types before expressing the logic. That makes programming much less fun.</p><p>Typescript is also a trans-piled language. It emits Javascript — something we already know, and we ought to know (to some extent at least). Another similar transpiled language I have used is Coffeescript. While coding in Coffeescript was much more fun, it was still hard to get away without learning Javascript altogether.</p><p>Fortunately with help from friends at <a href="http://chennai-js.org/">Chennai-js</a> and <a href="https://www.meetup.com/Angular-Chennai-Meetup/">Angular-Chennai</a> communities, I was able to get past the prejudices. Here are some ideas that helped.</p><h3>Its just Javascript</h3><p>Is what my friend <a href="https://github.com/anishbenji">Anish Benjamin</a> always said. The point is that you can start writing Typescript gradually. There’s no need for an upfront investment in learning. At first, the code may look entirely like Javascript —that is just fine.</p><pre>function<em> hello () {<br>  </em>return &#39;Hello world&#39;<br>}</pre><p>That is a valid Typescript program.</p><p>Also, much of modern Javascript is supported. If you’re a fan of ES-6 features like arrow functions, you don’t have to give up on those (<a href="http://kangax.github.io/compat-table/es6/#typescript">currently supported features</a>)</p><pre>let hello = () =&gt; &#39;Hello World&#39;</pre><h3>Type annotations <em>actually </em>help catch bugs</h3><p>The kind of bugs I was hoping to catch are the function return type bugs. This is the one problem I consistently struggled with in Ruby also. I try to make sure my methods/functions have a return value belonging to a single data type. The following is a code smell (to me)-</p><pre>function foo(x){<br>  if (x==0) return 123;<br>  return &#39;123&#39;;<br>}</pre><p>Any code here that invokes <strong><em>foo</em></strong> has too be aware of different kinds of return values it gives. And sooner or later, I’d forget that and write code that assumes <strong><em>foo</em></strong> returns a string only.</p><p>This has been the biggest win with type annotations. The compiler points out whenever we break our return-type contract.</p><pre>function foo (x:number): string {<br>  if (x==0) return 123; // &lt;- Typescript shows error<br>  return &#39;123&#39;<br>}</pre><p>So now I can tell the future self -</p><ul><li>The function returns a string. All code using it can be sure of that.</li><li>The function expects arguments of number type. Don’t expect type-casting magic anywhere. Pass a number only.</li><li>If you are planning to change the type contract, things will break. And the compiler will probably tell you that.</li></ul><h3>Types help with data structures</h3><p>This is another area I found lack of types frustrating. The following example is from a program dealing with a tree (the data structure). And here’s a function that returns a new node of the tree -</p><pre>function make_node(data, parent) {<br>  return { <br>    data: data,<br>    parent: parent,  // reference to the parent node<br>    children: []     // list of child nodes<br>  }<br>}</pre><p>This is quick and minimal, but also inadequate. What I need is -</p><ul><li>Say explicitly that parent is also a tree-node</li><li>Say explicitly that the children list contains the tree-node objects only.</li><li>A separate place to declare the shape of my node (similar a Struct in C)</li></ul><p>Typescript comes with an elegant solution —an <strong>interface</strong></p><pre>interface MyNode {<br>  data: String,<br>  parent: MyNode,<br>  children: MyNode[]<br>}</pre><pre>function make_node(data, parent:MyNode): Mynode {<br>  return {<br>    data: data,<br>    parent: parent,<br>    children: []<br>  }<br>}</pre><p>The interface definition makes my data structure unambiguous. And it also helps us prevent mistakes like adding wrong data to children</p><pre>node.children.push(&#39;123&#39;) // wrong<br>node.children.push({ data: &#39;123&#39; }) // wrong<br>node.children.push({ data: &#39;123&#39;, parent: &#39;wrong-parent&#39; }) // wrong</pre><h3>About null</h3><p>By default, Typescript’s treatment of <strong>null</strong> (and <strong>undefined</strong>) is a little liberal. All value, of any type are allowed to hold a null.</p><pre>function foo (x:number): string {<br>  if (x === 0) return 123;       // &lt;- Typescript complains<br>  if (x === 1) return null;      // &lt;- Fine<br>  if (x === 2) return undefined; // &lt;- Fine<br>  return &#39;123&#39;<br>}</pre><p>But that is only by default. By enabling a the <em>strictNullChecks</em>, the type checking policy can be made more strict (<a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html">Typescript 2 onwards</a>). With that, it starts warning where-ever we try to use null or undefined with values of other types.</p><h3>Other pleasant surprises</h3><h4>The VS Code editor</h4><p>The best typescript programming experience comes with the VS Code editor. Not only does it have out-of-the-box support for type feedback, it also comes with a powerful debugger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4cDlaOgpBa9q5F1juQ4HNQ.png" /></figure><p>Before this, the only other Javascript debugger I used was the Chrome Devtools (and Firebug). And I found debugging Javascript out of the browser challenging, as I’m new to Node.js tooling. VS Code debugger was a godsend.</p><h4>React support</h4><p>Typescript is well supported with React-js. In-fact Create-React-App, the popular React boilerplate <a href="https://github.com/Microsoft/TypeScript-React-Starter#typescript-react-starter">can be used with Typescript</a> fairly easily. Frameworks like <a href="https://vuejs.org/v2/guide/typescript.html">Vue</a> and <a href="https://electronjs.org/blog/typescript">Electron</a> also have official Typescript support.</p><h3>Conclusion</h3><p>I would highly recommend trying out Typescript on an active project. The coding experience is delightful, and the learning curve is smooth and comfortable. Support is available in plenty. And the code produced is far more robust and confident than plain Javascript.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff973d3311cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>