<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ flowtype - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ flowtype - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 18 Jun 2026 05:25:50 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/flowtype/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ You Already Use Types - So Here's Why You Should Use a Type System ]]>
                </title>
                <description>
                    <![CDATA[ By swyx This post is for skeptics and newcomers to type systems, and aims to articulate rather than hard sell. First we'll look at how static type conventions appear in your dynamically typed coding. Then we'll step back and try to think about what ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/you-already-use-types/</link>
                <guid isPermaLink="false">66d46154d14641365a05097b</guid>
                
                    <category>
                        <![CDATA[ flowtype ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ TypeScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 29 Aug 2019 15:41:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9ca094740569d1a4ca4989.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By swyx</p>
<p>This post is for skeptics and newcomers to type systems, and aims to articulate rather than hard sell.</p>
<ol>
<li>First we'll look at how static type conventions appear in your dynamically typed coding.</li>
<li>Then we'll step back and try to think about what this phenomenon tells us about how we want to code.</li>
<li>Lastly, we'll ask some (leading!) questions that should arise from these insights.</li>
</ol>
<h2 id="heading-1a-types-in-names">1A: Types in Names</h2>
<p>Regardless of language, your journey with types starts almost as soon as you learn to code. The basic list data structure invites a corresponding plural:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> dog = <span class="hljs-string">'Fido'</span>
<span class="hljs-keyword">var</span> dogs = [<span class="hljs-string">'Fido'</span>, <span class="hljs-string">'Sudo'</span>, <span class="hljs-string">'Woof'</span>]
</code></pre>
<p>As you work with more and more and more code, you start to form opinions that you may mandate to your team or style guide:</p>
<ul>
<li>always use specific names like <code>dogID</code> vs <code>dogName</code> vs <code>dogBreed</code> or a namespace/class/object like <code>dog.name</code> or <code>dog.id</code> or <code>dog.breed</code></li>
<li>singles should not be substrings of plurals, e.g. BAD: <code>blog</code> and <code>blogs</code>, GOOD: <code>blogPost</code> vs <code>blogList</code></li>
<li>booleans <a target="_blank" href="https://github.com/typescript-eslint/typescript-eslint/issues/515">should have a boolean-ish prefix</a>, like <code>isLoading</code>, <code>hasProperty</code>, <code>didChange</code></li>
<li>functions with side effects should have verbs</li>
<li>internal variables should have a <code>_prefix</code></li>
</ul>
<p>This may seem trivial since we're talking about variable names, but this vein runs <em>extremely</em> deep. Names in our coding reflect the concepts and constraints we place on our code to make it more maintainable at scale:</p>
<ul>
<li><a target="_blank" href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0">Presentational Components vs Stateful/Connected Containers</a></li>
<li><a target="_blank" href="http://bradfrost.com/blog/post/atomic-web-design/">Atoms, Molecules, Organisms, Templates, Pages</a></li>
<li><a target="_blank" href="https://reactjs.org/blog/2016/09/28/our-first-50000-stars.html#api-churn">Concepts, Actions, Operands</a> (one of the most successful name grammars ever)</li>
<li><a target="_blank" href="http://getbem.com/naming/">Block__Element--Modifier</a></li>
<li><a target="_blank" href="https://reactjs.org/docs/higher-order-components.html">Higher Order Components</a></li>
</ul>
<p>These all seep into your code accordingly: <code>*Container</code>, <code>*Component</code>, <code>*Reducer</code>, <code>*Template</code>, <code>*Page</code>, <code>with*</code>.</p>
<p>Once you start crossing execution paradigms, you start feeling your way into monadic type hints.</p>
<p>Node.js felt this early on:</p>
<pre><code class="lang-js">fs.readFile(myfile, callback)
fs.readFileSync(myfile) <span class="hljs-comment">// introduced when people realized callback hell might not be worth non-blocking</span>
</code></pre>
<p>React introduced the <code>use</code> prefix to indicate hooking into the runtime that must respect <a target="_blank" href="https://reactjs.org/docs/hooks-rules.html">certain rules</a>:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Component</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [bool, setBool] = React.useState(<span class="hljs-literal">true</span>)
  React.useEffect(callback)
  <span class="hljs-keyword">const</span> foo = useCustomHook()
  <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>I am personally fond of reminders of nullability:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> maybeResult = <span class="hljs-keyword">await</span> fetchAPI()
<span class="hljs-keyword">if</span> (maybeResult) {
  <span class="hljs-keyword">const</span> result = maybeResult
  <span class="hljs-comment">// do things with result</span>
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">// maybeResult is falsy, dont assume it is there</span>
}
</code></pre>
<p><strong>In almost everything you name, you're already using types.</strong></p>
<p>So what, you ask?</p>
<p>Keep reading, I'm building up to it.</p>
<h2 id="heading-1b-types-in-data-structures">1B: Types in Data Structures</h2>
<p>The problem with encoding types in names is that the language probably doesn't care about your meticulously named variables (indeed, in JavaScript, it probably gets mercilessly minified beyond recognition). It will happily run your code and throw a runtime error if you forget to respect your own nametypehints. What if we made types formally checkable through data structures?</p>
<p>The most basic are constants. In Redux, it is <a target="_blank" href="https://decembersoft.com/posts/a-simple-naming-convention-for-action-creators-in-redux-js/">common to explicitly (and redundantly) set SCREAMING_CASE_CONSTANTS</a>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> ADD_TODO = <span class="hljs-string">'slice/ADD_TODO'</span>

<span class="hljs-comment">// later in redux code:</span>
<span class="hljs-keyword">import</span> { ADD_TODO } <span class="hljs-keyword">from</span> <span class="hljs-string">'./redux/types'</span>
<span class="hljs-keyword">switch</span> (action.type) {
  <span class="hljs-keyword">case</span> ADD_TODO:
  <span class="hljs-comment">// do stuff based on the action</span>
  <span class="hljs-comment">// ...</span>
}
</code></pre>
<p>This is mostly done because you can't trust your fellow developer not to typo their strings.</p>
<p>However even these strings offer too much trust, and we found it important enough to add a new language feature to guarantee uniqueness:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> ADD_TODO = <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">'slice/ADD_TODO'</span>)
</code></pre>
<p>We also fake our way toward enums this way:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> colors = {
  <span class="hljs-attr">BLUE</span>: <span class="hljs-built_in">Symbol</span>(<span class="hljs-number">1</span>),
  <span class="hljs-attr">GREEN</span>: <span class="hljs-built_in">Symbol</span>(<span class="hljs-number">2</span>),
  <span class="hljs-attr">RED</span>: <span class="hljs-built_in">Symbol</span>(<span class="hljs-number">3</span>),
}
</code></pre>
<p>But simple values (strings, numbers, booleans) are actually easy to compare and treat accordingly.</p>
<p>More pressing is encoding types in complex values.</p>
<p>This usually happens when you have arrays of objects and the objects are different in some ways and similar in others:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> animals = [{ <span class="hljs-attr">name</span>: <span class="hljs-string">'Fido'</span>, <span class="hljs-attr">legs</span>: <span class="hljs-number">4</span>, <span class="hljs-attr">says</span>: <span class="hljs-string">'woof'</span> }, { <span class="hljs-attr">name</span>: <span class="hljs-string">'Kermit'</span>, <span class="hljs-attr">legs</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">marriedTo</span>: <span class="hljs-string">'Piggy'</span> }]
<span class="hljs-comment">// will have bugs if an animal with both `says` and `marriedTo` exists</span>
animals.forEach(<span class="hljs-function">(<span class="hljs-params">animal</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (animal.says) {
    <span class="hljs-comment">// i guess it's a dog?</span>
  }
  <span class="hljs-keyword">if</span> (animal.marriedTo) {
    <span class="hljs-comment">// i guess it's a frog?</span>
  }
})
</code></pre>
<p>Buggy checking and implicitly assumed types is often a cause for much pain. Better to type explicitly:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> animals = [
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'dog'</span>, <span class="hljs-comment">// new!</span>
    <span class="hljs-attr">name</span>: <span class="hljs-string">'Fido'</span>,
    <span class="hljs-attr">legs</span>: <span class="hljs-number">4</span>,
    <span class="hljs-attr">says</span>: <span class="hljs-string">'woof'</span>,
  },
  {
    <span class="hljs-attr">type</span>: <span class="hljs-string">'frog'</span>, <span class="hljs-comment">// new!</span>
    <span class="hljs-attr">name</span>: <span class="hljs-string">'Kermit'</span>,
    <span class="hljs-attr">legs</span>: <span class="hljs-number">2</span>,
    <span class="hljs-attr">marriedTo</span>: <span class="hljs-string">'Piggy'</span>,
  },
]
animals.forEach(<span class="hljs-function">(<span class="hljs-params">animal</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (animal.type === <span class="hljs-string">'dog'</span>) {
    <span class="hljs-comment">// must be a dog!</span>
  }
  <span class="hljs-keyword">if</span> (animal.type === <span class="hljs-string">'frog'</span>) {
    <span class="hljs-comment">// must be a frog!</span>
  }
})
</code></pre>
<p>This is in fact what happens for Redux (and, interestingly enough, handy for other things like <a target="_blank" href="https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html">Discriminated Unions</a>), but you will see this <strong>everywhere</strong> in <a target="_blank" href="https://github.com/sw-yx/overreacted.io/blob/master/gatsby-config.js#L25-L50">Gatsby</a> and <a target="_blank" href="https://babeljs.io/docs/en/plugins/#plugin-options">Babel</a> and <a target="_blank" href="https://reactjs.org/docs/react-without-jsx.html">React</a> and I'm sure you know of cases I don't.</p>
<p>Types even exist in HTML: <code>&lt;input type="file"&gt;</code> and <code>&lt;input type="checkbox"&gt;</code> behave so differently! (and I already mentioned Types in CSS with <a target="_blank" href="http://getbem.com/naming/">Block__Element--Modifier</a>)</p>
<p><strong>Even in HTML/CSS, you're already using types.</strong></p>
<h2 id="heading-1c-types-in-apis">1C: Types in APIs</h2>
<p>I'm almost done. Even outside your programming language, the interfaces between machines involve types.</p>
<p>REST's big innovation was basically a primitive form of typing client-server requests: <code>GET</code>, <code>PUT</code>, <code>POST</code>, <code>DELETE</code>. Web conventions have introduced other type fields in requests, like the <code>accept-encoding</code> header, that you must adhere to to get what you want. However, RESTfulness is basically not enforced, and because it doesn't offer guarantees, downstream tooling cannot assume properly behaved endpoints.</p>
<p>GraphQL takes that idea and dials it up to 11: Types are key to queries and mutations and fragments, but also on every field and every input variable, validated on both clientside and serverside by spec. With much stronger guarantees, it is able to ship <a target="_blank" href="https://github.com/graphql/graphiql">much better tooling</a> as a community norm.</p>
<p>I don't know the history of SOAP and XML and gRPC and other machine-machine communication protocols but I'm willing to bet there are strong parallels.</p>
<h2 id="heading-part-2-what-does-this-tell-us">Part 2: What Does This Tell Us?</h2>
<p>This was a very long, and yet inexhaustive examination of types permeating everything you do. Now that you've seen these patterns, you can probably think of more examples I'm forgetting right now. But at every turn, it seems the way toward more maintainable code, and better tooling is to add types in some way.</p>
<p>I mentioned parts of this thesis in <a target="_blank" href="https://www.swyx.io/writing/how-to-name-things">How To Name Things</a>, but basically all of the naming schemas fall under an enlightened form of Hungarian notation, as described in Joel Spolsky's <a target="_blank" href="https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/">Making Wrong Code Look Wrong</a>.</p>
<p>If none of what I have described resonates with you, and isn't something you've already been doing, then types may not be for you.</p>
<p>But if it does, and you've been doing this in slipshod fashion, you may be interested in more structure around how you use types in your code, and in using better tooling that takes advantage of all the hard work you already put into types.</p>
<p>You may be working your way toward a type system, without even knowing it.</p>
<h2 id="heading-part-3-leading-questions">Part 3: Leading Questions</h2>
<p>So knowing what we know now about using types in our code without a type system. I'll ask some hard questions.</p>
<p><strong>Question 1: What do you currently do to enforce types without a type system?</strong></p>
<p>At an individual level, you engage in defensive coding and manual verification. Basically manually eyeballing your own code and reflexively adding checks and guards without knowing if they're really needed (or, worse, NOT doing it and figuring out after seeing run time exceptions).</p>
<p>At a team level, you spend multiples of developer-hours in code review, inviting bike shedding over names, which we all know is great fun.</p>
<p>These two processes are manual methods, and a very poor use of developer time. <a target="_blank" href="https://hackernoon.com/dont-be-the-bad-cop-in-pull-request-reviews-let-software-do-that-job-1eb9e574c2d1">Don't be the bad cop</a> - this wrecks team dynamics. At scale, you are mathematically guaranteed to have lapses in code quality (therefore causing production bugs), either because everyone missed something, or there just wasn't enough time and you just had to ship something, or there wasn't a good enough policy in place yet.</p>
<p>The solution, of course, is to automate it. As Nick Schrock says, <a target="_blank" href="https://medium.com/@schrockn/on-code-reviews-b1c7c94d868c">Delegate to Tooling Whenever Possible</a>. Prettier and ESLint help to hold up your code quality - only to the extent to which the program can understand you based on an AST. It does not offer any help crossing function and file boundaries - if function <code>Foo</code> expects 4 arguments and you only pass it 3, no linter will yell at you and you'll have to defensively code inside <code>Foo</code>.</p>
<p>So there's only so much you can automate with a linter. What about the rest you can't automate?</p>
<p>Therein lies the last option: Do Nothing.</p>
<p>Most people do nothing to enforce their informally designed type systems.</p>
<p><strong>Question 2: How much of these types are you writing yourself?</strong></p>
<p>It goes without saying that if all your type policies are created by you, then they must be written by you and enforced by you.</p>
<p>That's totally different from how we write code today. We lean heavily on open source - <a target="_blank" href="https://mobile.twitter.com/housecor/status/1078634947831914496">97% of modern web app code is from npm</a>. We import shared code, and then write the last mile parts that make our app special (aka business logic).</p>
<p>Is there a way to share types?</p>
<p>(<a target="_blank" href="https://github.com/DefinitelyTyped/DefinitelyTyped/">yes</a>)</p>
<p><strong>Question 3: What if your types were standardized?</strong></p>
<p>Research has shown that the #1 reason programmers adopt a language is the existing capabilities and functionality available for them to use. I will learn Python to use TensorFlow. I will learn Objective C to create native iOS experiences. Correspondingly, JS has been so successful because it runs everywhere, compounded by the wide availability of free open source software written <em>by other people</em>. With some standardized type system, we can <a target="_blank" href="https://github.com/DefinitelyTyped/DefinitelyTyped/">import types just as easily as we import open source software</a> written by other people.</p>
<p>Just like GraphQL vs REST, Standardized types in a language unlock much better tooling. I will offer 4 examples:</p>
<p><strong>Example 1: Faster Feedback</strong></p>
<p>We might take months and days to learn from <strong>runtime errors</strong>, and these are exposed to users, so they are the worst possible outcome.</p>
<p>We write tests and apply lint rules and other checks to move these errors to <strong>build time errors</strong>, which shortens feedback cycles to minutes and hours. (As I wrote recently: <a target="_blank" href="https://css-tricks.com/types-or-tests-why-not-both/">Types don't replace Tests!</a>)</p>
<p>Type Systems can shorten this feedback by yet another order of magnitude, to seconds, checking during <strong>write time</strong>. (Linters can also do this. Both are conditional on a supportive IDE like VS Code) As side effect, you get autocomplete for free, because autocomplete and write time validation are two sides of the same coin.</p>
<p><strong>Example 2: Better Error Messages</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Foo = {
  getData() {
    <span class="hljs-keyword">return</span> <span class="hljs-string">'data'</span>
  },
}
Foo[<span class="hljs-string">'getdata'</span>]() <span class="hljs-comment">// Error: undefined is not a function</span>
</code></pre>
<p>JavaScript is intentionally lazy evaluation by design. Instead of the dreaded and nondescript <code>undefined is not a function</code> during runtime, we can move this to write time. Here's the write time error message for the exact same code:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> Foo = {
  getData() {
    <span class="hljs-keyword">return</span> <span class="hljs-string">'data'</span>
  },
}
Foo[<span class="hljs-string">'getdata'</span>]() <span class="hljs-comment">// Property 'getdata' does not exist on type '{ getData(): string; }'. Did you mean 'getData'?</span>
</code></pre>
<p>Why yes, TypeScript, I did.</p>
<p><strong>Example 3: Edge Case Exhaustion</strong></p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> fruit: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>
fruit.toLowerCase() <span class="hljs-comment">// Error: Object is possibly 'undefined'.</span>
</code></pre>
<p>Over and above the built in nullable checking (which takes care of issues like passing in 3 arguments when a function expects 4), a type system can make the most of your enums (aka union types). I struggled coming up with a good example but here is one:</p>
<pre><code class="lang-ts"><span class="hljs-keyword">type</span> Fruit = <span class="hljs-string">'banana'</span> | <span class="hljs-string">'orange'</span> | <span class="hljs-string">'apple'</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeDessert</span>(<span class="hljs-params">fruit: Fruit</span>) </span>{
  <span class="hljs-comment">// Error: Not all code paths return a value.</span>
  <span class="hljs-keyword">switch</span> (fruit) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">'banana'</span>:
      <span class="hljs-keyword">return</span> <span class="hljs-string">'Banana Shake'</span>
    <span class="hljs-keyword">case</span> <span class="hljs-string">'orange'</span>:
      <span class="hljs-keyword">return</span> <span class="hljs-string">'Orange Juice'</span>
  }
}
</code></pre>
<p><strong>Example 4: Fearless Refactoring</strong></p>
<p>Many people mentioned this and I'll be honest that it took me a long while to come around to this. The thinking is: "so what? I don't refactor that much. so that means TypeScript's benefit is smaller to me than to you because I'm better than you."</p>
<p>This is the wrong take.</p>
<p>When we start off exploring a problem, we start off with a vague idea of the solution. As we progress, we learn more about the problem, or priorities change, and unless we've done it a million times we probably picked something wrong along the way, whether it be function API, data structure, or something larger scale.</p>
<p><img src="http://www.methodsandtools.com/archive/refact8.png" alt="refact8" width="600" height="400" loading="lazy"></p>
<p>The question is then to either stick with it until it breaks or to refactor the moment you can sense that you're going to outgrow whatever you used to have. I'll assume you accept that there are often benefits to refactoring. So why do we avoid refactoring?</p>
<p><strong>The reason you put off that refactor is that it is costly, not because it isn't beneficial to you. Yet putting it off only increases future cost.</strong></p>
<p>Type System tooling helps to dramatically lower the cost of that refactor, so you can experience the benefits earlier. It lowers that cost via faster feedback, exhaustiveness checking, and better error messages.</p>
<h2 id="heading-truth-in-advertising">Truth in Advertising</h2>
<p>There is a cost to learning Type Systems you didn't write. This cost may offset any imagined benefit to automated type checking. This is why I put a great deal of effort into helping to lower that learning curve. However, be aware that it is a new language and will involve unfamiliar concepts, and also that even the tooling is an imperfect work in progress.</p>
<p>But it is good enough for <a target="_blank" href="https://www.reddit.com/r/typescript/comments/aofcik/38_of_bugs_at_airbnb_could_have_been_prevented_by/">AirBnb</a> and <a target="_blank" href="http://neugierig.org/software/blog/2018/09/typescript-at-google.html">Google</a> and <a target="_blank" href="https://github.com/atlassian/react-beautiful-dnd/issues/982">Atlassian</a> and <a target="_blank" href="https://eng.lyft.com/typescript-at-lyft-64f0702346ea">Lyft</a> and <a target="_blank" href="https://medium.com/priceline-labs/trying-out-typescript-part-1-15a5267215b9">Priceline</a> and <a target="_blank" href="https://slack.engineering/typescript-at-slack-a81307fa288d">Slack</a> and it may be for you.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Why use static types in JavaScript? (A 4-part primer on static typing with Flow) ]]>
                </title>
                <description>
                    <![CDATA[ By Preethi Kasireddy As a JavaScript developer, you can code all day long without encountering any static types. So why bother learning about them? Well it turns out learning types isn’t just an exercise in mind-expansion. If you’re willing to invest... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/why-use-static-types-in-javascript-part-1-8382da1e0adb/</link>
                <guid isPermaLink="false">66c3672eb737bb2ce7073223</guid>
                
                    <category>
                        <![CDATA[ Computer Science ]]>
                    </category>
                
                    <category>
                        <![CDATA[ flowtype ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 08 Dec 2016 17:46:02 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*YJbUzd6InXYN8EGTIGEFOA.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Preethi Kasireddy</p>
<p>As a JavaScript developer, you can code all day long without encountering any static types. So why bother learning about them?</p>
<p>Well it turns out learning types isn’t just an exercise in mind-expansion. If you’re willing to invest some time in learning about static types’ advantages, disadvantages, and use cases, it could help your programming immensely.</p>
<p>Interested? Well you’re in luck — that’s what the rest of this four-part series is about.</p>
<h3 id="heading-first-a-definition">First, a definition</h3>
<p>The quickest way to understand static types is to contrast them with dynamic types. A language with static types is referred to as a <strong>statically-typed language</strong>. On the other hand, a language with dynamic types is referred to as a <strong>dynamically-typed</strong> language.</p>
<p>The core difference is that <strong>statically-typed</strong> languages perform type checking at <strong>compile time</strong>, while <strong>dynamically-typed</strong> languages perform type checking at <strong>runtime</strong>.</p>
<p>This leaves one more concept for you to tackle: what does “<strong>type-checking”</strong> mean?</p>
<p>To explain, let’s look at types in Java versus Javascript.</p>
<p><strong>“Types”</strong> refers to the type of data being defined.</p>
<p>For example, in Java if you define a <code>boolean</code> as:  </p>
<p><code>boolean result = true;</code></p>
<p>This has a correct type, because the <code>boolean</code> annotation matches the value given to <code>result</code>, as opposed to an integer or anything else.</p>
<p>On the other hand, if you tried to declare:  </p>
<p><code>boolean result = 123;</code></p>
<p>…this would fail to compile because it has an incorrect type. It explicitly marks <code>result</code> as a <code>boolean</code>, but then defines it as the integer <code>123</code>.</p>
<p>JavaScript and other dynamically-typed languages have a different approach, allowing the context to establish what type of data is being defined:</p>
<p><code>var result = true;</code></p>
<p>Long story short: statically-typed languages require you to declare the data types of your constructs before you can use them. Dynamically-typed languages do not. JavaScript implies the data type, while Java states it outright.</p>
<p>So as you can see, types allow you to specify program <strong>invariants</strong>, or the logical assertions and conditions under which the program will execute.</p>
<p><strong>Type-checking</strong> verifies and enforces that the type of a construct (constant, boolean, number, variable, array, object) matches an invariant that you’ve specified. You might, for example, specify that “this function always returns a string.” When the program runs, you can safely assume that it will return a string.</p>
<p>The differences between static type checking and dynamic type checking matter most when a type error occurs. In a statically-typed language, type errors occur during the compilation step, that is, at compile time. In dynamically-typed languages, the errors occur only once the program is executed. That is, at <strong>runtime</strong>.</p>
<p>This means that a program written in a dynamically-typed language (like JavaScript or Python) can compile even if it contains type errors that would otherwise prevent the script from running properly.</p>
<p>On the other hand, if a program written in a statically-typed language (like Scala or C++) contains type errors, it will fail to compile until the errors have been fixed.</p>
<h3 id="heading-a-new-era-of-javascript">A new era of JavaScript</h3>
<p>Because JavaScript is a dynamically-typed language, you can go about declaring variables, functions, objects and anything without declaring the type.</p>
<p>Convenient, but not always ideal. Which is why tools like <a target="_blank" href="https://flowtype.org">Flow</a> and <a target="_blank" href="https://www.typescriptlang.org/">TypeScript</a> have recently stepped in to give JavaScript developers the <em>option</em> to use static types.</p>
<p><a target="_blank" href="https://flowtype.org/"><strong>Flow</strong></a> is an open-source static type checking library developed and released by Facebook that allows you to incrementally add types to your JavaScript code.</p>
<p><a target="_blank" href="https://www.typescriptlang.org/"><strong>TypeScript</strong></a>, on the other hand, is a superset that compiles down to JavaScript — although it feels almost like a new statically-typed language in its own right. That said, it looks and feels very similar to JavaScript and isn’t hard to pick up.</p>
<p>In either case, when you want to use types, you explicitly tell the tool about which file(s) to type-check. For TypeScript you do this by writing files with the <code>.ts</code> extension instead of <code>.js</code>. For Flow, you include a comment on top of the file with <code>@flow</code></p>
<p>Once you’ve declared that you want to type-check a file, you get to use their respective syntax for defining types. One distinction to make between the two tools is that Flow is a type “checker” and not a compiler. TypeScript, on the other hand, is a compiler.</p>
<p>I truly believe that tools like Flow and TypeScript present a <em>generational shift and advancement</em> for JavaScript.</p>
<p>Personally, I’ve learned so much by using types in my day-to-day. Which is why I hope you’ll join me on this short and sweet journey into static types.</p>
<p>The rest of this 4-part post will cover:</p>
<p>Part I. <a target="_blank" href="https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-1-8382da1e0adb#.prtc7vhrr">A quick intro to the Flow syntax and language</a></p>
<p>Parts II &amp; III. <a target="_blank" href="https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-2-part-3-be699ee7be60#.9ywoivqaz">Advantages &amp; Disadvantages of static types (with detailed walk-through examples)</a></p>
<p>Part IV. <a target="_blank" href="https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-4-b2e1e06a67c9#.cb2z6mty8">Should you use static types in JavaScript or not?</a></p>
<p>Note that I chose Flow over TypeScript in the examples in this post because of my familiarity with it. For your own purposes, please do research and pick the right tool for you. TypeScript is also fantastic.</p>
<p>Without further ado, let’s begin!</p>
<h3 id="heading-part-1-a-quick-intro-to-flow-syntax-and-language">Part 1: A quick intro to Flow syntax and language</h3>
<p>To understand the advantages and disadvantages of static types, you should first get a basic understanding of the syntax for static types using Flow. If you’ve never worked in a statically-typed language before, the syntax might take a little while to get used to.</p>
<p>Let’s begin by exploring how to add types to JavaScript primitives, as well as constructs like Arrays, Object, Functions, and etc.</p>
<h4 id="heading-boolean">boolean</h4>
<p>This describes a <code>boolean</code> (true or false) value in JavaScript.</p>
<p>Notice that when you want to specify a type, the syntax you use is:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*Z79CcJO6h4DO_xKJMdK9zg.png" alt="Image" width="800" height="328" loading="lazy"></p>
<h4 id="heading-number">number</h4>
<p>This describes an IEEE 754 floating point number. Unlike many other programming languages, JavaScript doesn’t define different types of numbers (like integers, short, long, and floating points). Instead, numbers are always stored as double precision floating point numbers. Hence, you only need one number type to define any number.</p>
<p><code>number</code> includes <code>Infinity</code> and <code>NaN</code>.</p>
<h4 id="heading-string">string</h4>
<p>This describes a string.</p>
<h4 id="heading-null">null</h4>
<p>This describes the <code>null</code> data type in JavaScript.</p>
<h4 id="heading-void">void</h4>
<p>This describes the <code>undefined</code> data type in JavaScript.</p>
<p>Note that <code>null</code> and <code>undefined</code> are treated differently. If you tried to do:</p>
<p>Flow would throw an error because the type <code>void</code> is supposed to be of type <code>undefined</code> which is not the same as the type <code>null</code>.</p>
<h4 id="heading-array">Array</h4>
<p>Describes a JavaScript array. You use the syntax <code>Array&lt;</code>;T&gt; to describe an array whose elements are of some type T.</p>
<p>Notice how I replaced <code>T</code> with <code>string</code>, which means I’m declaring <code>messages</code> as an array of strings.</p>
<h4 id="heading-object">Object</h4>
<p>This describes a JavaScript object. There are a few different ways to add types to objects.</p>
<p>You could add types to describe the shape of an object:</p>
<p>You could define objects as maps where you map a string to some value:</p>
<p>You could also define an object as an <code>Object</code> type:</p>
<p>This last approach lets us set any key and value on your object without restriction, so it doesn’t really add much value as far as type-checking is concerned.</p>
<h4 id="heading-any">any</h4>
<p>This can represent literally any type. The <code>any</code> type is effectively unchecked, so you should try to avoid using it unless absolutely necessary (like when you need to opt out of type checking or need an escape hatch).</p>
<p>One situation you might find <code>any</code> useful for is when using an external library that extends another system’s prototypes (like Object.prototype).</p>
<p>For example, if you are using a library that extends Object.prototype with a <code>doSomething</code> property:</p>
<p>You may get an error:</p>
<p>To circumvent this, you can use <code>any</code>:</p>
<h4 id="heading-functions">Functions</h4>
<p>The most common way to add types to functions is to add types to it’s input arguments and (when relevant) the return value:</p>
<p>You can even add types to async functions (see below) and generators:</p>
<p>Notice how our second parameter <code>getPurchaseLimit</code> is annotated as a function that returns a <code>Promise</code>. And <code>amountExceedsPurchaseLimit</code> is annotated as also returning a <code>Promise</code>.</p>
<h4 id="heading-type-alias">Type alias</h4>
<p>Type aliasing is one of my favorite ways to use static types. They allow you to use existing types (number, string, etc.) to compose new types:</p>
<p>Above, I created a new type called <code>PaymentMethod</code> which has properties that are comprised of <code>number</code> and <code>string</code> types.</p>
<p>Now if you want to use the <code>PaymentMethod</code> type, you can do:</p>
<p>You can also create type aliases for any primitive by wrapping the underlying type inside another type. For example, if you want to type alias a <code>Name</code> and <code>EmailAddress</code>:</p>
<p>By doing this, you’re indicating that <code>Name</code> and <code>Email</code> are distinct things, not just strings. Since a name and email aren’t really interchangeable, doing this prevents you from accidentally mixing them up.</p>
<h4 id="heading-generics">Generics</h4>
<p>Generics are a way to abstract over the types themselves. What does this mean?</p>
<p>Let’s take a look:</p>
<p>I created an abstraction for the type <code>T</code>. Now you can use whatever type you want to represent <code>T</code>. For <code>numberT</code>, <code>T</code> was of type <code>number</code>. Meanwhile, for <code>arrayT</code>, T was of type <code>Array&lt;numb</code>er&gt;.</p>
<p>Yes, I know. It’s dizzying stuff if this is the first time you’re looking at types. I promise the “gentle” intro is almost over!</p>
<h4 id="heading-maybe">Maybe</h4>
<p>Maybe type allows us to type annotate a potentially <code>null</code> or <code>undefined</code> value. They have the type <code>T|void|null</code> for some type <code>T</code>, meaning it is either type <code>T</code> or it is <code>undefined</code> or <code>null</code>. To define a <code>maybe</code> type, you put a question mark in front of the type definition:</p>
<p>Here I’m saying that message is either a <code>string</code>, or it’s <code>null</code> or <code>undefined</code>.</p>
<p>You can also use maybe to indicate that an object property will be either of some type <code>T</code> or <code>undefined</code>:</p>
<p>By putting the <code>?</code> next to the property name for <code>middleInitial</code>, you can indicate that this field is optional.</p>
<h4 id="heading-disjoint-unions">Disjoint unions</h4>
<p>This is another powerful way to model data. Disjoint unions are useful when you have a program that needs to deal with different kinds of data all at once. In other words, the shape of the data can be different based on the situation.</p>
<p>Extending on the <code>PaymentMethod</code> type from our earlier generics example, let’s say that you have an app where users can have one of three types of payment methods. In this case, you can do something like:</p>
<p>Then you can define your PaymentMethod type as a disjoint union with three cases.</p>
<p>Payment method now can only ever be one of these three shapes. The property <code>type</code> is the property that makes the union type “disjoint”.</p>
<p>You’ll see more practical examples of disjoint union types later in part II.</p>
<p>All right, almost done. There are a couple other features of Flow worth mentioning before concluding this intro:</p>
<p><strong>1) Type inference</strong>: Flow uses type inference where possible. Type inference kicks in when the type checker can automatically deduce the data type of an expression. This helps avoid excessive annotation.</p>
<p>For example, you can write:</p>
<p>Even though this Class doesn’t have types, Flow can adequately type check it:</p>
<p>Here I’ve tried to define <code>area</code> as a <code>string</code>, but in the <code>Rectangle</code> class definition we defined <code>width</code> and <code>height</code> as numbers. So based on the function definition for <code>area</code>, it must be return a <code>number</code>. Even though I didn’t explicitly define types for the <code>area</code> function, Flow caught the error.</p>
<p>One thing to note is that the Flow maintainers recommend that if you were exporting this class definition, you’d want to add explicit type definitions to make it easier to find the cause of errors when the class is not used in a local context.</p>
<p><strong>2) Dynamic type tests</strong>: What this basically means is that Flow has logic to determine what the the type of a value will be at runtime and so is able to use that knowledge when performing static analysis. They become useful in situations like when Flow throws an error but you need to convince flow that what you’re doing is right.</p>
<p>I won’t go into too much detail because it’s more of an advanced feature that I hope to write about separately, but if you want to learn more, it’s worth reading through the <a target="_blank" href="https://flowtype.org/docs/dynamic-type-tests.html">docs</a>.</p>
<h3 id="heading-were-done-with-syntax">We’re done with syntax</h3>
<p>We covered a lot of ground in one section! I hope this high-level overview has been helpful and manageable. If you’re curious to go deeper, I encourage you to dive into the <a target="_blank" href="https://flowtype.org/docs/">well-written docs</a> and explore.</p>
<p>With syntax out of the way, let’s finally get to the fun part: <a target="_blank" href="https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-2-part-3-be699ee7be60#.9ywoivqaz">exploring the advantages and disadvantages of using types</a>!</p>
<p>Next up: <a target="_blank" href="https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-2-part-3-be699ee7be60#.9ywoivqaz">Part 2 &amp; 3</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Setting up Flow when you’ve already got Babel in place ]]>
                </title>
                <description>
                    <![CDATA[ By Jamie Kyle Flow is a static type checker for JavaScript. It makes you more productive by providing feedback as you write code. Flow gives you warnings in real-time that point out when you’ve made a mistake. If you would like to know more, check ou... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/using-flow-with-babel-c04fdca8d14d/</link>
                <guid isPermaLink="false">66c3646309a9333511bcdb2c</guid>
                
                    <category>
                        <![CDATA[ flowtype ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 09 Aug 2016 14:53:46 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*S3mUOCWvhWPralT0YbcdRA.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Jamie Kyle</p>
<p>Flow is a static type checker for JavaScript. It makes you more productive by providing feedback as you write code. Flow gives you warnings in real-time that point out when you’ve made a mistake. If you would like to know more, check out <a target="_blank" href="https://flowtype.org/">flowtype.org</a>.</p>
<p>Rather than trying to build it’s own entirely separate ecosystem, Flow hooks into the existing JavaScript ecosystem. Using Babel to compile your code is one of the easiest ways to integrate Flow into a project.</p>
<p>After two years of hard work, Babel works pretty much everywhere, just take a look at the setup page with integrations for <a target="_blank" href="http://babeljs.io/docs/setup/">every build tool you can imagine</a>.</p>
<p>If you don’t have Babel set up yet, you can use that guide to get set up. You also might want to checkout my <a target="_blank" href="https://github.com/thejameskyle/babel-handbook">handbook on Babel</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/jnjy7OosWCrCZ9O8fqF3Bg8NbCPnggn804el" alt="Image" width="800" height="112" loading="lazy"></p>
<h3 id="heading-setting-up-flow-on-top-of-babel">Setting up Flow on top of Babel</h3>
<p>Once you have Babel set up, it’s easy to get going with Flow. First lets install two dependencies:</p>
<pre><code>$ npm install --save-dev babel-plugin-transform-flow-strip-types$ npm install --<span class="hljs-built_in">global</span> flow-bin
</code></pre><p>The Babel plugin is there in order to strip Flow types away so that your program runs. <strong>flow-bin</strong> is how you use Flow from the command line.</p>
<p>Next, let’s add the Babel plugin to your <strong>.babelrc</strong> (or where-ever you are configuring Babel with options):</p>
<pre><code>{  <span class="hljs-attr">presets</span>: [...],  <span class="hljs-attr">plugins</span>: [..., <span class="hljs-string">"transform-flow-strip-types"</span>]}
</code></pre><blockquote>
<p><strong>Note:</strong> I’m assuming you are using Babel 6 for this tutorial. It is recommended that you <a target="_blank" href="http://babeljs.io/blog/2015/10/31/setting-up-babel-6">upgrade</a> if you have not already.</p>
</blockquote>
<p>Finally we’ll run <strong>flow init</strong> in our directory, which will create a <strong>.flowconfig</strong> file that should look like this:</p>
<pre><code>[ignore]
</code></pre><pre><code>[include]
</code></pre><pre><code>[libs]
</code></pre><pre><code>[options]
</code></pre><p>Awesome! Now we have Flow all set up in your project. How about we start making it run on some files?</p>
<h3 id="heading-getting-flow-running">Getting Flow running</h3>
<p>Flow is designed to be introduced to your repo incrementally. That means that you don’t have to add it to your whole codebase all at once. Instead, you can add it file-by-file as you go. Let’s start with something simple to get you going.</p>
<p>First, try to find a file that doesn’t have a lot of complexity or external dependencies. Something with just a handful of plain functions to get started with.</p>
<pre><code><span class="hljs-keyword">import</span> {getNumberFromString} <span class="hljs-keyword">from</span> <span class="hljs-string">"string-math-lib"</span>;
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyStrings</span>(<span class="hljs-params">a, b</span>) </span>{  <span class="hljs-keyword">return</span> getNumberFromString(a) * getNumberFromString(b);}
</code></pre><p>In order to get Flow running on this file, we need to add a “flow pragma” comment at the top like this:</p>
<pre><code><span class="hljs-comment">// @flow</span>
</code></pre><pre><code><span class="hljs-keyword">import</span> {getNumberFromString} <span class="hljs-keyword">from</span> <span class="hljs-string">"string-math-lib"</span>;
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyStrings</span>(<span class="hljs-params">a, b</span>) </span>{  <span class="hljs-keyword">return</span> getNumberFromString(a) * getNumberFromString(b);}
</code></pre><p>This little comment at the top of the file tells Flow “Okay, I want you to type-check this file”.</p>
<p>Now we need to actually run Flow for the first time. In order to do that, you’ll need to switch back to your terminal and run the following command:</p>
<pre><code>$ flow
</code></pre><blockquote>
<p><strong>Note:</strong> This command is an alias of <strong>flow status</strong>.</p>
</blockquote>
<p>What this command does is start up a Flow server and asks it for the “status” of your repo, which will most likely return some errors for you to fix.</p>
<p>The most common errors that you’re gonna see in a new file are:</p>
<ul>
<li>“Missing annotation”</li>
<li>“Required module not found”</li>
</ul>
<p>These errors are related to imports and exports. The reason they come up is a result of how Flow works. In order to check types across files, Flow looks directly at the imports and exports of each file.</p>
<h4 id="heading-missing-annotation"><strong>“Missing annotation”</strong></h4>
<p>You’ll see an error like this from Flow because it relates somehow to an export of your file. Other than that Flow won’t complain about missing type annotations.</p>
<p>So in order to fix that, we can start adding some types to your file. For a detailed guide on how to do that <a target="_blank" href="https://flowtype.org/docs/type-annotations.html">see the user guide</a>. What you should end up is with some types like this:</p>
<pre><code><span class="hljs-keyword">import</span> {getNumberFromString} <span class="hljs-keyword">from</span> <span class="hljs-string">"string-math-lib"</span>;
</code></pre><pre><code><span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">multiplyStrings</span>(<span class="hljs-params">a: string, b: string</span>): <span class="hljs-title">number</span> </span>{  <span class="hljs-keyword">return</span> getNumberFromString(a) * getNumberFromString(b);}
</code></pre><p>Keep running <strong>flow</strong> as you add your types to see the effects of what you are doing. Eventually you should be able to clear out all the “Missing annotation” errors.</p>
<h4 id="heading-required-module-not-found">“Required module not found”</h4>
<p>You’ll get these errors whenever you an import/require that can’t be resolved using Node’s normal module algorithm or if you have ignored it with your <strong>.flowconfig</strong>.</p>
<p>This can be caused by a lot of things, maybe you’re using a special webpack resolver, maybe you forgot to install something. Whatever the reason, Flow needs to be able to find the module you are importing to do its job correctly. You have a couple options on how to solve this:</p>
<ol>
<li><strong>module.name_mapper —</strong> specify a regular expression to match against module names, and a replacement pattern.</li>
<li>Create a library definition for the missing module</li>
</ol>
<p>We’ll focus on creating a library definition for the module, if you need to use <strong>module.name_mapper</strong> you can see more about it <a target="_blank" href="https://flowtype.org/docs/advanced-configuration.html#options">in the documentation</a>.</p>
<h4 id="heading-creating-a-library-definition">Creating a library definition</h4>
<p>Having library definitions is useful for giving types to the packages you have installed that don’t have types. Let’s set one up for our <strong>string-math-lib</strong> library from the previous example.</p>
<p>First create a <strong>flow-typed</strong> directory in your root directory (the directory where you put your <strong>.flowconfig</strong>).</p>
<blockquote>
<p>You can use other directory names using the <strong>[lib]</strong> section of your <strong>.flowconfig</strong>. However, using <strong>flow-typed</strong> will work out of the box.</p>
</blockquote>
<p>Now we’ll create a <strong>flow-typed/string-math-lib.js</strong> file to hold our “libdef” and start it off like this:</p>
<pre><code>declare <span class="hljs-built_in">module</span> <span class="hljs-string">"string-math-lib"</span> {  <span class="hljs-comment">// ...}</span>
</code></pre><p>Next we just need to write definitions for exports of that module.</p>
<pre><code>declare <span class="hljs-built_in">module</span> <span class="hljs-string">"string-math-lib"</span> {  declare <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getNumberFromString</span>(<span class="hljs-params">str: string</span>): <span class="hljs-title">number</span>}</span>
</code></pre><blockquote>
<p><strong>Note:</strong> If you need to document the “default” or primary export, you can do that with <strong>declare module.exports:</strong> or <strong>declare export default</strong></p>
</blockquote>
<p>There’s a lot more to library definitions so you should read through the <a target="_blank" href="https://flowtype.org/docs/declarations.html">documentation</a> and read <a target="_blank" href="https://medium.com/@thejameskyle/flow-mapping-an-object-373d64c44592">this blog post on how to create high-quality libdefs</a>.</p>
<h4 id="heading-installing-a-libdef-from-flow-typed">Installing a libdef from flow-typed</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/fnjErEoc73SGhKGGham9ABRSZpDjgRv9hk96" alt="Image" width="800" height="204" loading="lazy"></p>
<p>Because libdefs can consume a lot of time, we maintain an official repository of high-quality libdefs for all sorts of packages called <a target="_blank" href="https://github.com/flowtype/flow-typed">flow-typed</a>.</p>
<p>To get started with flow-typed, install the command line interface (CLI) globally:</p>
<pre><code>$ npm install --<span class="hljs-built_in">global</span> flow-typed
</code></pre><p>Now you can look within <a target="_blank" href="https://github.com/flowtype/flow-typed/tree/master/definitions/npm"><strong>flow-typed/definitions/npm</strong></a> to see if there’s an existing libdef for a package you want to use, if there is you can install it like this:</p>
<pre><code>$ flow-typed install chalk@<span class="hljs-number">1.0</span><span class="hljs-number">.0</span> --flowVersion <span class="hljs-number">0.30</span>
</code></pre><p>This tells flow-typed that you want to install the <strong>chalk</strong> package at the <strong>1.0.0</strong> version when you are running Flow <strong>0.30</strong>.</p>
<p>The <strong>flow-typed</strong> CLI is still in beta and there is a lot of improvements planned for it, so expect this to improve a lot in the near future.</p>
<p>Be sure to contribute back to the flow-typed libdefs. It’s a community effort, and the more people that contribute, the better it gets.</p>
<h4 id="heading-other-errors-you-might-run-into">Other errors you might run into:</h4>
<p>Hopefully, we’ve covered just about everything that you will run into while getting started with Flow. However, you also might run into some errors like this:</p>
<ul>
<li>Package inside of <strong>node_modules</strong> is reporting errors</li>
<li>Reading <strong>node_modules</strong> is taking a really long time</li>
<li>Malformed JSON inside <strong>node_modules</strong> is causing errors</li>
</ul>
<p>There’s a handful of reasons for these types of errors that I won’t get into in this post (I’m working on detailed documentation for each individual error). For now, in order to keep yourself moving, you can just <strong>[ignore]</strong> the files that are causing these errors.</p>
<p>This means adding file paths to your <strong>[ignore]</strong> section in your <strong>.flowconfig</strong> like this:</p>
<pre><code>[ignore]./node_modules/package-name<span class="hljs-comment">/*./node_modules/other-package/tests/*.json</span>
</code></pre><pre><code>[include]
</code></pre><pre><code>[libs]
</code></pre><pre><code>[options]
</code></pre><p>There is generally better options than this, but this should give you a good place to start.</p>
<blockquote>
<p>For some examples of how to better handle errors within node_modules see this <a target="_blank" href="http://stackoverflow.com/questions/38225538/flow-type-checker-errors-in-node-modules/38264353#38264353">Stack Overflow answer about fbjs</a>.</p>
</blockquote>
<h4 id="heading-pro-tip-checking-to-see-how-well-youre-covered"><strong>Pro tip: Checking to see how well you’re covered</strong></h4>
<p>If you’re ever wondering how well a file is covered by Flow, you can use the following command to see a coverage report:</p>
<pre><code>$ flow coverage path/to/file.js --color
</code></pre><h3 id="heading-additional-resources-and-support">Additional Resources and Support</h3>
<p>There’s lots that was not covered by this article. So here are some links to resources that can help you out.</p>
<ul>
<li><a target="_blank" href="https://flowtype.org/">Flow Website</a></li>
<li><a target="_blank" href="https://flowtype.org/try/">Try Flow Online</a></li>
<li><a target="_blank" href="https://github.com/facebook/flow">Flow GitHub</a></li>
<li><a target="_blank" href="http://stackoverflow.com/questions/tagged/flowtype">Stack Overflow #flowtyped</a></li>
</ul>
<p>The Flow team is committed to making sure that everyone has an excellent experience using Flow. If that is ever not true, we’d love to hear from you about how to improve.</p>
<p>Follow <a target="_blank" href="https://twitter.com/thejameskyle">James Kyle on twitter</a>. Follow <a target="_blank" href="https://twitter.com/flowtype">Flow on twitter</a> too.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
