<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kajetan Świątek</title>
    <description>The latest articles on DEV Community by Kajetan Świątek (@kajetansw).</description>
    <link>https://dev.to/kajetansw</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F316410%2Fc282096a-84dc-4866-b4c2-0d64286a6d48.jpg</url>
      <title>DEV Community: Kajetan Świątek</title>
      <link>https://dev.to/kajetansw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kajetansw"/>
    <language>en</language>
    <item>
      <title>From Angular to React - switching jobs, switching technologies</title>
      <dc:creator>Kajetan Świątek</dc:creator>
      <pubDate>Fri, 16 Apr 2021 13:02:38 +0000</pubDate>
      <link>https://dev.to/kajetansw/from-angular-to-react-switching-jobs-switching-technologies-32l2</link>
      <guid>https://dev.to/kajetansw/from-angular-to-react-switching-jobs-switching-technologies-32l2</guid>
      <description>&lt;p&gt;⚡ Originally published on &lt;a href="https://kajetan.dev/2021/from-angular-to-react/"&gt;kajetan.dev&lt;/a&gt;. ⚡&lt;/p&gt;




&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;I still like Angular, but I fell in love with React and its ecosystem (tooling and libraries), simplicity (concept of components as functions from the state to UI), and flexibility (you can set up and tune your project as you see fit).&lt;/p&gt;




&lt;h2&gt;
  
  
  How did this decision come to be?
&lt;/h2&gt;

&lt;p&gt;After 2.5 years of working as a front-end developer, I decided to look for a new job. It was a decision driven by my desire to learn and grow.&lt;/p&gt;

&lt;p&gt;I came to the conclusion that changing perspectives in the early stage of your career seems healthy. Participating in different projects lets you learn a lot about different approaches companies take, solutions they use, and practices they follow.&lt;/p&gt;

&lt;p&gt;This approach is a representation of the "perspective-driven self-development" &lt;a href="https://kajetan.dev/2020/perspective-driven-self-development/"&gt;I wrote about&lt;/a&gt; some time ago.&lt;/p&gt;

&lt;p&gt;From day one that I decided to look for a job I knew I wanted to work with React (and overall different technologies than before, at best) and strived towards this goal. I did that despite the number of Angular offers I was bombarded every week and companies rejecting me because of my lack of experience with this technology.&lt;/p&gt;

&lt;p&gt;I started to learn React not long after starting working with Angular. I did it in my free time, just out of curiosity. During job interviews, it was hard to convince people that I have the abilities needed for the job. Every time I felt somewhat ashamed and tired repeating the same dialogue over and over:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Interviewer:&lt;/em&gt;&lt;br&gt; "So, you want to work with React, but you have no commercial experience?"&lt;br&gt;&lt;br&gt;
&lt;em&gt;Me:&lt;/em&gt;&lt;br&gt; "Unfortunately not... Only the things I've learned myself."&lt;br&gt;&lt;br&gt;
(... And after the further elaboration on my ways of learning the technical interview started.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's when I thought about ways of proving myself and strengthening my knowledge even more. I decided to use the oldest trick in the world - I started a side-project. But not a typical one - a one that, by the end, will be a full-blown product. Thoroughly designed, implemented, and finally - deployed to production where everybody would see it. (I almost completed it so no announcements for now!)&lt;/p&gt;

&lt;p&gt;But let me get to the point. I want to share with you my thoughts on the technicalities behind my decision. I want to compare Angular and React and share my point of view with you.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I don't hate about Angular?
&lt;/h2&gt;

&lt;p&gt;Let me get this straight - I still like Angular! It didn't bore or anger me in any way so that I decided to leave it behind. I don't cast aside the idea of me coming back to it one day.&lt;/p&gt;

&lt;p&gt;What's good about Angular? It is still in the top 3 of most popular JS frameworks and not without a reason. It has a rich ecosystem of external libraries for solving problems related to web applications, enterprise or not (state management, component libraries, internationalization, etc.), tooling to make it easier and automate what's repeatable (Angular CLI, schematics, and CDK), and an active community.&lt;/p&gt;

&lt;p&gt;Another thing is its opinionated architecture and tooling. Components, modules, services, etc. are separate "architecture pieces" by design and our projects (with its file structure) should also follow this "modularity" defined by the framework. Angular also provides solutions for solving typical problems concerning web applications - making HTTP calls, handling forms, linting, creating unit tests, etc. (Although, you can always opt-out and use other things.)&lt;/p&gt;

&lt;p&gt;Why is it good to use something opinionated, like Angular? Because some architectural decisions are just there, already defined, ready to use. We don't have to bother making things up and worrying whether they will prove useful in the long term. I personally know a team that decided to use Angular just because of that and Angular proved to be a perfect choice for them. After a couple of years of development, they are still satisfied.&lt;/p&gt;

&lt;p&gt;What also I absolutely adore is Angular's TypeScript integration out-of-the-box. I believe in TypeScript and like to use it in every project. &lt;/p&gt;




&lt;h2&gt;
  
  
  What do I love about React?
&lt;/h2&gt;

&lt;p&gt;Angular and React are fundamentally different things - one is a framework, other is a library.  Angular provides us tooling and certain patterns that we have to follow in order to create applications. React is different, it handles only a layer of our applications, that is responsible for rendering the UI.&lt;/p&gt;

&lt;p&gt;So, what is the first reason why I fell in love with React so that I decided to "abandon" Angular? The reason is... That React is NOT opinionated.&lt;/p&gt;

&lt;p&gt;Now I stirred things up! Let me explain - being "opinionated" is a power, but also a curse at the same time. It depends on perspective. Some people would find it refreshing to be stripped of some responsibilities, like coming up with a technological stack or project's architecture. On the other side of the barricade are people who enjoy the freedom of choosing their perfect and polished stack and practices, refined over the years.&lt;/p&gt;

&lt;p&gt;How does it look with React? The structure of our project, what tooling to use, how to make HTTP calls, which testing framework to use - things like that are totally up to us. Which approach is better? That depends. (Haha.) That depends on your needs, experience and overall "beliefs".&lt;/p&gt;

&lt;p&gt;From my point of view, after 2.5 years of working in front-end, I feel I started to mature and to establish my own ways I want to do things. There is nothing more enjoyable for me than working daily with a stack you feel the most powerful. (Recently I fell in love with a &lt;a href="https://nextjs.org/"&gt;NextJS&lt;/a&gt; + &lt;a href="https://chakra-ui.com/"&gt;ChakraUI&lt;/a&gt; + &lt;a href="https://firebase.google.com/?hl=pl"&gt;Firebase&lt;/a&gt; + &lt;a href="https://cloudinary.com/"&gt;Cloudinary&lt;/a&gt; combination.)&lt;/p&gt;

&lt;p&gt;I also fell in love with React's functional components. The sheer idea of components being functions from the state to UI is very simple and powerful. React Hooks also feel like a good way to encapsulate and reuse logic throughout your applications. (Although some people that think those are bloated and incomprehensible in large projects, but I'm too inexperienced to have my own opinion about that.)&lt;/p&gt;




&lt;h2&gt;
  
  
  The present and the future
&lt;/h2&gt;

&lt;p&gt;Like you can see, there is nothing I despise about Angular. I just happened to be enchanted by the concepts behind React so much, that it felt like a natural switch for me.&lt;/p&gt;

&lt;p&gt;What I want to do now is to dive into React's ecosystem more and more and work with some talented people at &lt;a href="https://www.softserveinc.com/en-us"&gt;SoftServe&lt;/a&gt; and &lt;a href="https://www.mural.co/"&gt;Mural&lt;/a&gt; who'll guide me into their ways of doing things.&lt;/p&gt;

</description>
      <category>career</category>
      <category>angular</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Perspective-Driven (Self) Development</title>
      <dc:creator>Kajetan Świątek</dc:creator>
      <pubDate>Mon, 11 May 2020 15:29:26 +0000</pubDate>
      <link>https://dev.to/kajetansw/perspective-driven-self-development-2fo9</link>
      <guid>https://dev.to/kajetansw/perspective-driven-self-development-2fo9</guid>
      <description>&lt;h2&gt;
  
  
  🎓 Introduction
&lt;/h2&gt;

&lt;p&gt;Perspective-Driven Self Development is a learning idea that can potentially enhance the way you acquire knowledge with increasing your confidence and sense of reliability along the way. Warning! Step with caution! The fun coming from it is highly addictive! &lt;/p&gt;

&lt;p&gt;Why "potentially", though? Because it is a way that may not be suitable for everyone. And it's fine! This is the way I do things personally and I wanted to share the concept with you. Nevertheless, you may approach things differently. The most important thing is to find a way that suits YOU best.&lt;/p&gt;

&lt;p&gt;The principle itself is pretty simple and let me emphasize it: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Try out new things! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's all, folks. ✋🎤&lt;/p&gt;

&lt;p&gt;Well, I didn't discover anything new, did I? We, developers, are constantly attracted to new, shiny ✨ things, which can be both good and bad. But I want to state it boldly here - there's nothing wrong with that! It's fun! It's educating! It's practical!&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚡ Why you (should) use it
&lt;/h2&gt;

&lt;p&gt;Speaking frankly, I'm fairly certain that you are already using this idea in your career as a tech person. You being here is enough proof! Reading articles like this, means that you saw it on Twitter or you follow my articles for some time (which would be pretty weird, because I don't think I'm that popular). That, in turn, means you strive to broaden your perspective by engaging someone else's articles, tutorials, researches, etc. You obviously want to improve and that's wonderful! &lt;/p&gt;

&lt;p&gt;Perspective-Driven Self Development is beneficial because with a wider perspective you start to see patterns. You've probably noticed by now, that practicing one OO language makes you understand another one waaay faster. The same is with libraries or frameworks that serve similar purposes. The same might be with cloud providers, databases, architectures, paradigms, languages, etc. Let's call those "&lt;strong&gt;learning domains&lt;/strong&gt;" from now on. &lt;/p&gt;

&lt;p&gt;Wider perspective boosts up your confidence - you seem to have more answers on all kinds of different questions because you've tried all kinds of different things! You won't have all the detailed knowledge in the world in your head, that's obvious. Although, there probably was a time when you came across a very difficult problem but managed to solve it by remembering, that someone, somewhere, solved the same problem. The piece of code/article/video you may have seen a couple of weeks ago while rummaging through the internet can change the game.&lt;/p&gt;

&lt;p&gt;Another thing is, that if you're just starting your journey in tech, this idea can help you get a sense of yourself. You'll start to get a hang of what are the things that bring you joy and you can just dive deeper into them. &lt;/p&gt;

&lt;p&gt;It's worth to mention that Perspective-Driven Development is highly compatible with the &lt;a href="https://www.swyx.io/writing/learn-in-public/"&gt;Learn in Public&lt;/a&gt; concepts by &lt;a href="https://twitter.com/swyx"&gt;swyx&lt;/a&gt;. While trying out new things and broadening your perspective you can use your newly acquired knowledge, e.g. build something fun and share it with others. You can do it via blog posts, talk at the meetup, a tutorial video - the floor is yours.&lt;/p&gt;




&lt;h2&gt;
  
  
  📜 Examples of learning domains
&lt;/h2&gt;

&lt;p&gt;When you start learning databases, you could start with MySQL. Then learn Oracle or PostgreSQL because... reasons. But wait, there are all sorts of databases! Non-relational, temporal, graph, client-side (those are just random examples). It may be cool to try out something different and check out what are pros and cons, use-cases, what fits you best etc. &lt;/p&gt;

&lt;p&gt;When starting with front-end development you have an enormously wide range of things you start from. &lt;a href="https://andreasbm.github.io/web-skills/"&gt;This page&lt;/a&gt; lets you see just how wide ("overwhelmingly" seems like a polite understatement 😨). The simplest example might just be when it comes to JavaScript frameworks. Once you've squeezed everything you can from framework X, you can try out framework Y - you might just see how much faster it takes to learn it.&lt;/p&gt;

&lt;p&gt;My personal favorite is a paradigm-shifting because it's the most challenging of them all. After years of learning one programming paradigm, it's hard to just shift your mind in another direction. The most popular example of that is learning FP after years of writing software in OOP (or another way around). After two years of programming in OOP (mostly Java and somewhat TypeScript) and learning all those principles (classes, inheritance, encapsulation, etc.) I've heard of this mythical Functional Programming and how complex but rewarding it is. I just couldn't help myself. I felt the challenge and couldn't wait to tackle this. After a year of learning FP, I still don't get a lot 😂 But I don't stop and every step forward feels rewarding. Let me tell you, when you feel excited just by a thought of trying out new things - you are a 100% follower of Perspective-Driven Self Development.&lt;/p&gt;




&lt;h2&gt;
  
  
  ⚠️ Mind the downsides
&lt;/h2&gt;

&lt;p&gt;Something can (and probably will) go wrong when practicing this approach. There may be a time when you devote your time to something and fail. An example of that might be discovering that some X bundler isn't as simple or "automagical" as it seemed and cannot be used for your project after two weeks of trying. But don't be discouraged - there's always some knowledge coming from a failure! The next time you test another bundler you might just know what to focus on and try it out in the early stages.&lt;/p&gt;

&lt;p&gt;Far more dangerous is taking this idea too far. If you spend too much time trying out different things, you might end up knowing too little of your main field of expertise. E.g. focusing on knowing different frameworks might end up with knowing 20% of each and that's not healthy for your career. Another, much broader, example is differentiating roles at your workplace. Should one person be a business analyst, a developer, and a tester at the same time? It's great to have knowledge of what each role is and what are their responsibilities but you probably should not do their job. That's too wide of a perspective to acquire by one person.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧘 Conclusion
&lt;/h2&gt;

&lt;p&gt;Considering the last part (the downsides) I'd love to give you some personal, concrete piece of advice - a recipe, a golden ratio, let's say. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pick up the learning domain you want to improve (e.g. JS framework), define the main branch that you should pursuit (like framework X, because you use it at work) and use 60-70% of your time to focus on that. The rest of the time you want to devote to the learning domain can be distributed among all other possibilities (framework Y, Z, etc.). It's healthier this way. Also mind that - the more you feel confident with your main learning domain, the more this ratio can drop, finding more time to learn new things.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hell, what else can I say, you should just try it out yourself (if you don't already)! Happy learning!&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>productivity</category>
      <category>learning</category>
      <category>career</category>
    </item>
    <item>
      <title>Discriminating parts of a union type</title>
      <dc:creator>Kajetan Świątek</dc:creator>
      <pubDate>Mon, 09 Mar 2020 16:25:50 +0000</pubDate>
      <link>https://dev.to/kajetansw/level-up-your-typescript-code-by-using-elm-part-2-27ca</link>
      <guid>https://dev.to/kajetansw/level-up-your-typescript-code-by-using-elm-part-2-27ca</guid>
      <description>&lt;p&gt;⚡️ Originally posted on my blog - &lt;a href="https://kajetan.dev"&gt;kajetan.dev&lt;/a&gt; ⚡️&lt;/p&gt;




&lt;p&gt;Within this mini-series, my goal is to convey to you my modest philosophy on how to improve both as a TypeScript developer and software engineer altogether. I decided to use a specific example for that: improving one's TypeScript code by trying out something else, not considered "mainstream", like Elm. &lt;/p&gt;

&lt;p&gt;I started by sharing with you the idea of Elm decoders and how they can be used in TypeScript.&lt;/p&gt;

&lt;p&gt;Now it's time to dive into other features of Elm and what can we learn from them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pattern matching - a million-dollar feature
&lt;/h2&gt;

&lt;p&gt;If you've used TypeScript before, you most likely came across switch-case statements. It's a quite useful tool in a programmers toolkit that enables check value of an expression against a specific &lt;code&gt;case&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's begin with a simple example and build our way up. Here's an example of a case expression in Elm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;parseRate&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="n"&gt;parseRate&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Horrible"&lt;/span&gt;
    &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bad"&lt;/span&gt;
    &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mediocre"&lt;/span&gt;
    &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Good"&lt;/span&gt;
    &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Excellent"&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INVALID RATE"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the most typical example I could think of but it lets you see for yourself how the syntax looks like. At the end, you can also see a &lt;code&gt;default&lt;/code&gt; option that is handled by using a &lt;code&gt;_&lt;/code&gt; wildcard. &lt;/p&gt;

&lt;p&gt;In most imperative languages that would be all we can squeeze from the case-statement. But Elm (and most of FP languages, in fact) can take it to another level. With it, you can match against even more complicated "patterns" like lists, tuples, records, etc. &lt;/p&gt;

&lt;p&gt;We'll start building an example for this section now. &lt;/p&gt;

&lt;p&gt;Let's assume, that our simple application loads a response from a server and renders it. The server can respond with either a successfully processed data (here, a simple integer value) or with validation errors. For that, we would like to define messages (actions that our application reacts to) and a model. &lt;/p&gt;

&lt;p&gt;We can discriminate three different messages and we can do it with a union of types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Ok&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Err&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;HttpError&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, we can create a corresponding model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;httpError&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application can carry an &lt;code&gt;Ok&lt;/code&gt; message, that indicates, that data has been fetched successfully. Another one is &lt;code&gt;Err&lt;/code&gt; that can carry a payload as a &lt;code&gt;List String&lt;/code&gt; (list of strings) - a list of validation errors that came as a result of our server-side computation. &lt;code&gt;HttpError&lt;/code&gt; contains data about possible HTTP error. &lt;/p&gt;

&lt;p&gt;Another thing is a &lt;code&gt;Model&lt;/code&gt; that our view is based on. In an ideal world that's how our web applications should behave - it should be a function that takes a model and returns a view based on that model. That's what we shall assume here (for simplicity) - that the type &lt;code&gt;Model&lt;/code&gt; represents both data that is carried by our application and also a view.&lt;/p&gt;

&lt;p&gt;Next, we would like to have a function, that transforms our model based on a received message. For that we would like to react to all of three possible messages available. Surprisingly, we can use the good, old case expression for that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;updateModel&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="n"&gt;updateModel&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
    &lt;span class="kt"&gt;Ok&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;Err&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;HttpError&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; 
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;httpError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It turns out, that case expressions in Elm can not only be used against primitive types but also against more complex ones. It is thanks to a mechanism known as the "pattern matching". &lt;/p&gt;

&lt;p&gt;With that we discriminate elements of a union of types, that is our &lt;code&gt;Msg&lt;/code&gt;, and return modified value of &lt;code&gt;Model&lt;/code&gt;. On &lt;code&gt;Ok&lt;/code&gt; we replace a &lt;code&gt;data&lt;/code&gt; field with response payload, on &lt;code&gt;Error&lt;/code&gt; we pass a list of error messages to the model. On &lt;code&gt;HttpError&lt;/code&gt; we pass an error message. &lt;/p&gt;

&lt;p&gt;Wait, that's not all! Would you like to see some cool trick? Let's just delete one case and see what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- MISSING PATTERNS -------------------------------------------- Jump To Problem

This `case` does not have branches for all possibilities:

27|&amp;gt;  case msg of
28|&amp;gt;    Ok data -&amp;gt;
29|&amp;gt;      { model | data = data }
30|&amp;gt;    Error es -&amp;gt; 
31|&amp;gt;      { model | error = es }

Missing possibilities include:

    HttpError _

I would have to crash if I saw one of those. Add branches for them!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The project won't even compile! Compiler reminds us (very politely 😉) that there are some missing patterns that we didn't handle and we should change that. Elm is designed like that - with a bunch of little helpers built within a compiler that watch over us and our types. With those, we are able to avoid many troublesome bugs. More than that - it was always a crucial point for Elm to have as helpful and readable error messages as possible. &lt;/p&gt;

&lt;p&gt;Unfortunately, JavaScript and TypeScript don't have a pattern matching feature. Even Elm's pattern matching is limited compared to Haskell, in favor of simplicity. But still - it is quite powerful.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;: &lt;a href="https://github.com/tc39/proposal-pattern-matching"&gt;Here&lt;/a&gt; you can read about pattern matching proposal for TS39 which is now at stage 1. Also, &lt;a href="http://learnyouahaskell.com/syntax-in-functions"&gt;find out more&lt;/a&gt; about pattern matching in Haskell (&lt;em&gt;this is a more advanced topic though&lt;/em&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nevertheless, it cannot discourage us - let's get inspired and try to simulate those concepts in TypeScript! First idea you may think of (at least I did) is to use string literal types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ok&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Err&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;HttpError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HttpError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Msg&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Ok&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Err&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;HttpError&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="nl"&gt;httpError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ok&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;// Well... What now?&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Err&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;// Well... What now?&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HttpError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;// Well... What now?&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, we quickly realize, that it's impossible for a string literal type to carry some extra payload. One of the popular ways to discriminate types that hold some payload is to define them as JS objects with an additional field (commonly named &lt;code&gt;type&lt;/code&gt;) that lets us discriminate object over the others:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Ok&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ok&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Err&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;HttpError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HttpError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="nl"&gt;httpError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Msg&lt;/span&gt;
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Ok&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Err&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;HttpError&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that we are able to successfully discriminate union of types with a case-statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ok&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Err&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;errors&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HttpError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;httpError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done! TypeScript compiler even helps us by merging possible values of a &lt;code&gt;type&lt;/code&gt; field from the union. When we hover over &lt;code&gt;msg.type&lt;/code&gt; we get all possible values: &lt;code&gt;(property) type: "Ok" | "Err" | "HttpError"&lt;/code&gt;. This can help us when building the list of our cases.&lt;/p&gt;

&lt;p&gt;But wait! What about a situation when we forget one of the possible cases like we did with the Elm example? &lt;/p&gt;

&lt;p&gt;It turns out that when you omit some cases (and do not provide a &lt;code&gt;default&lt;/code&gt; option) we get a compilation error of: &lt;code&gt;Function lacks ending return statement and return type does not include 'undefined'&lt;/code&gt;. &lt;code&gt;undefined&lt;/code&gt;? We don't want that in our function! This is a message for us that we missed a &lt;code&gt;case&lt;/code&gt; and should add one. &lt;/p&gt;

&lt;p&gt;Look out, though! There are some cases when that magic won't work and we will expose ourselves to unexpected bugs as a result. Above will work only if you have &lt;code&gt;strictNullChecks&lt;/code&gt; flag turned on within your &lt;code&gt;tsconfig.json&lt;/code&gt; (which I strongly encourage you to do!) and return type of your function is not &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;void&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Fortunately, there is one interesting solution that is universal and works in all situations. It lets us leverage &lt;code&gt;never&lt;/code&gt; type within our case expression:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ok&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Err&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;errors&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;HttpError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;httpError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;assertUnreachable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;assertUnreachable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Supposed to be unreachable...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;never&lt;/code&gt; is a type that: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"(...) represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns."&lt;/em&gt; &lt;br&gt;
~ TypeScript docs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here, when we remove one case, for example the &lt;code&gt;HttpError&lt;/code&gt; one, we get a compilation error of &lt;code&gt;Argument of type 'HttpError' is not assignable to parameter of type 'never'.&lt;/code&gt;. It's because it is impossible to assign a value to type &lt;code&gt;never&lt;/code&gt;, which has no values at all.&lt;/p&gt;

&lt;p&gt;There is only one more thing worth noting. Discrimination of a union of types in TypeScript works only within the top-level fields of an object!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;: &lt;a href="https://blog.asana.com/2020/01/typescript-quirks/"&gt;This blog post&lt;/a&gt; describes interesting quirks of a TypeScript, that are all worth to know. Among them are constraints that come with discrimination of union type I mentioned above.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Pattern matching is a killer feature of such languages like Haskell and PureScript and most of the developers wouldn't imagine working without it. It exist also in Elm and it lets us, for example, discriminate actions that occur in our applications. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;: If you want to read about pattern matching in Elm, you can find more at &lt;a href="https://elmprogramming.com/pattern-matching.html"&gt;this section&lt;/a&gt; of "Beginning Elm" tutorial.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;This is all for this part! This series turns out to be longer than I anticipated when I started conceptualizing it. Nevertheless, I hope that it'll turn out useful to anyone that wants to learn more about TypeScript and/or consider trying out some other languages (with stronger type system).&lt;/p&gt;

&lt;p&gt;Thanks for reading and see you next time! &lt;/p&gt;

</description>
      <category>typescript</category>
      <category>elm</category>
      <category>functional</category>
    </item>
    <item>
      <title>Type safety at runtime with JSON decoders</title>
      <dc:creator>Kajetan Świątek</dc:creator>
      <pubDate>Wed, 12 Feb 2020 16:22:02 +0000</pubDate>
      <link>https://dev.to/kajetansw/level-up-your-typescript-code-by-using-elm-part-1-4o52</link>
      <guid>https://dev.to/kajetansw/level-up-your-typescript-code-by-using-elm-part-1-4o52</guid>
      <description>&lt;p&gt;⚡️ Originally posted on my blog - &lt;a href="https://kajetan.dev" rel="noopener noreferrer"&gt;kajetan.dev&lt;/a&gt; ⚡️&lt;/p&gt;




&lt;p&gt;I imagine what you may think right now after reading name of this blog series - "Dude, you're crazy! TypeScript is complex on its own and you just said that I should learn another language? Forget it! I revoke the subscription!".&lt;/p&gt;

&lt;p&gt;Let me explain first what is the purpose of this article before you actually do that. Seriously, don't, please.&lt;/p&gt;

&lt;p&gt;😉&lt;/p&gt;




&lt;h2&gt;
  
  
  What's the purpose? 🤔
&lt;/h2&gt;

&lt;p&gt;I use TypeScript daily and I think it is beneficial to sharpen your tool as good and as frequently as you can. For this, I seek means to improve my work and my understanding of deep-down mechanisms. Along the way, when you have enough understanding of a tool, you may want to try another one. This way you can experience something new and compare it with your previous knowledge. The findings are often surprising!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fkajetansw%2Fkajetan-dev-blog%2Fmaster%2Fcontent%2Fposts%2F2020%2Flevel-up-your-typescript-code-by-using-elm%2Fmarkus-spiske.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fkajetansw%2Fkajetan-dev-blog%2Fmaster%2Fcontent%2Fposts%2F2020%2Flevel-up-your-typescript-code-by-using-elm%2Fmarkus-spiske.jpg" alt="Set of tools laid in a box."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same goes for programming languages. When the time comes, you'd probably think about learning something new. Who knows - maybe that new tool may become your new favorite...&lt;/p&gt;

&lt;p&gt;Basically, what I want to tell you is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Knowing your tool is as important as developing a different &lt;strong&gt;perspective&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There was a time when I wanted to learn something new and enter this yet-again-hyped "functional programming train", so I decided to learn &lt;a href="https://elm-lang.org/" rel="noopener noreferrer"&gt;Elm&lt;/a&gt;. This front-end language is recommended for all people with a JavaScript background, so it suited me perfectly.&lt;/p&gt;

&lt;p&gt;It turns out, that I didn't fall in love with Elm (I like it though!), nor I want to use it everywhere from now on. Nevertheless, I came to some interesting conclusions and gained a different perspective on the tools I was using earlier. &lt;/p&gt;

&lt;p&gt;In just a second I'll share with you just how knowing Elm lead me to writing better TypeScript code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: I don't want to promote myself as being an Elm expert. The truth is that I fiddled with it only for a couple of weeks by creating a small project. Even though, if you have any questions, you are more than welcome to ask 😊 In the worst case - I would just admit that I don't know, fairly embarrassed! 😧&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Type-checking - know the quirks 🚩
&lt;/h2&gt;

&lt;p&gt;How you can call yourself a true master of given language if you are not aware of its downsides? And let's be honest here - TypeScript is not perfect. It is a weird case though because a lot of its quirks come from the fact that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the end, TypeScript compiles down to JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means, that after the compilation phase, we forget about all the types, interfaces, etc. we created during our work. The powerful type system delivered by TypeScript is thrown away and that may lead to all sorts of bugs and inconsistencies during runtime.&lt;/p&gt;

&lt;p&gt;The simplest example is handling HTTP responses from REST API. Let's imagine a simple function that returns data on a planet using Star Wars API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchFilm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filmId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://swapi.co/api/films/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;filmId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having this function written like that, the type of the function is inferred to be &lt;code&gt;number -&amp;gt; Promise&amp;lt;any&amp;gt;&lt;/code&gt;. We can do better with our types!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// We create an interface for our expected value:&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;SWFilm&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;episode_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;opening_crawl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// We specify the type of our output:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchFilm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filmId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SWFilm&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://swapi.co/api/films/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;filmId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Let's simulate some operation being done to our fetched value:&lt;/span&gt;
&lt;span class="nf"&gt;fetchFilm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;film&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;film&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opening_crawl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)...}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OUTPUT: It is a period of civil war. Rebel spac...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yup, we are safe now! Both TypeScipt compiler and other developers reading the code know what is the expected type of &lt;code&gt;fetchPlanet&lt;/code&gt;'s output. &lt;/p&gt;

&lt;p&gt;Should we really feel safe, though?&lt;/p&gt;

&lt;p&gt;What if our app runs happily on the production and the external REST API changes its model without a notice? What if we don't talk to our back-end developers anymore and they are the ones that changed the model? (No, really, talk to each other! 😏)&lt;/p&gt;

&lt;p&gt;In our case, if the &lt;code&gt;opening_crawl&lt;/code&gt; field may change to a camel case notation (obviously!) and we are prone to receiving everyone's favorite &lt;code&gt;TypeError: Cannot read property 'substring' of undefined&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;What we realize pretty quickly then, is that: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TypeScript does not provide any type-checking during runtime. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Many people starting with the language have the wrong impression of how the TS compiler works and don't know about that important fact.&lt;/p&gt;

&lt;p&gt;What can we do then? We can do simple type checking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;fetchFilm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;film&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;film&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;film&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opening_crawl&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Opening: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;film&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;opening_crawl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;...`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wrong type, dude!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt; 
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, it's not all that bad. But what if we have to check if the entire object is compliant with the interface we've created? That may prove to be tedious 😧&lt;/p&gt;

&lt;p&gt;A solution to our problem may lay in the common mechanism in Elm - JSON decoders.&lt;/p&gt;

&lt;p&gt;JSON Decoder is a function that takes data in JSON format and extracts specified fields from it. Let's see how this looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Decode&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decodeString&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;decodeString&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4"&lt;/span&gt;     
&lt;span class="c1"&gt;-- OUTPUT: Ok 4 &lt;/span&gt;

&lt;span class="n"&gt;decodeString&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;haw haw"&lt;/span&gt;     
&lt;span class="c1"&gt;-- OUTPUT: Err ...  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like you see, nothing to be afraid of. Firstly, in Elm, we don't use parenthesis when calling the function. &lt;code&gt;decodeString&lt;/code&gt; is a build-in function for decoding strings in JSON format. It accepts a specific decoder and the value to be decoded. &lt;code&gt;int&lt;/code&gt; is a primitive decoder for integers, so what we're doing above is trying to parse two strings to an integer. As you see, one attempt is successful and the second is not. &lt;/p&gt;

&lt;p&gt;We now know how to parse primitive values. What about JSON objects? For that we have to create the appropriate decoder ourselves:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: We won't dive into too many details in our examples (considering that this may be your first time seeing Elm code) but if you want to check it out for yourself, I'll provide you with appropriate resources. I'll just try to keep the examples as simple as I can 😉&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Decode&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;Decode&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Decoder&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Decode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Pipeline&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;SWFilm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;episode_id&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opening_crawl&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;swFilmDecoder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Decoder&lt;/span&gt; &lt;span class="kt"&gt;SWFilm&lt;/span&gt;
&lt;span class="n"&gt;swFilmDecoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;Decode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;succeed&lt;/span&gt; &lt;span class="kt"&gt;SWFilm&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title"&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;episode_id"&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;
        &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;opening_crawl"&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;: You can find more specific information on how to use and create decoders by reading &lt;a href="https://elmprogramming.com/decoding-json-part-1.html" rel="noopener noreferrer"&gt;section of Beggining Elm tutorial&lt;/a&gt; or docs for the &lt;a href="https://package.elm-lang.org/packages/NoRedInk/elm-json-decode-pipeline/latest/Json-Decode-Pipeline" rel="noopener noreferrer"&gt;NoRedInk/elm-json-decode-pipeline&lt;/a&gt; package. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Firstly, we create type alias for &lt;code&gt;SWFilm&lt;/code&gt;, similar to the interface created with TypeScript.&lt;/p&gt;

&lt;p&gt;When comes to creating a decoder, in a typical manner for Elm, we first declare type and then define the body of a constant or a function.&lt;/p&gt;

&lt;p&gt;We could interpret the body of a &lt;code&gt;swFilmDecoder&lt;/code&gt; as listing out required fields for our &lt;code&gt;SWFilm&lt;/code&gt; type and their types individually. By doing that we compose more primitive decoders, like &lt;code&gt;string : Decoder String&lt;/code&gt;, to create more complex Decoders for more complex data structures.&lt;/p&gt;

&lt;p&gt;Then, if we want to recreate our previous &lt;code&gt;fetchFilm&lt;/code&gt; function, it would look like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Http&lt;/span&gt;

&lt;span class="n"&gt;fetchFilm&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Cmd&lt;/span&gt; &lt;span class="kt"&gt;Msg&lt;/span&gt;
&lt;span class="n"&gt;fetchFilm&lt;/span&gt; &lt;span class="n"&gt;filmId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="kt"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://swapi.co/api/films/"&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="n"&gt;filmId&lt;/span&gt;
    &lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expectJson&lt;/span&gt; &lt;span class="n"&gt;toMsgOnSuccessOrError&lt;/span&gt; &lt;span class="n"&gt;swFilmDecoder&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;: You can find more information on using &lt;code&gt;Http&lt;/code&gt; module and creating HTTP requests by reading &lt;a href="https://elmprogramming.com/fetching-data-using-get.html" rel="noopener noreferrer"&gt;section of Beginning Elm tutorial&lt;/a&gt; or reading &lt;a href="https://guide.elm-lang.org/effects/http.html" rel="noopener noreferrer"&gt;the official guide&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this, we create command (that's the meaning of &lt;code&gt;Cmd Msg&lt;/code&gt; in the type signature) for &lt;a href="https://elmprogramming.com/elm-runtime.html" rel="noopener noreferrer"&gt;Elm runtime&lt;/a&gt; for running HTTP GET request for given URL and expecting response body to be in JSON format. We also specify that JSON we receive in the response should be compatible with &lt;code&gt;SWFilm&lt;/code&gt; type by providing our &lt;code&gt;swFilmDecoder&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The most enigmatic thing here is probably &lt;code&gt;toMsgOnSuccessOrError&lt;/code&gt; function. I won't dive into it too deeply, because that is not the point here. Let's agree, that this custom function should create an appropriate message to the Elm runtime after parsing JSON through the decoder - whether it succeeds or fails to do so. &lt;/p&gt;

&lt;p&gt;By creating HTTP client this way, we are basically enforced by the type system to create not only a decoder that checks our JSON structure but also a concrete function that reacts to success or failure of the decoding process (here &lt;code&gt;toMsgOnSuccessOrError&lt;/code&gt;). And that's what is missing in our TypeScript code.&lt;/p&gt;

&lt;p&gt;Should we build that kind of system in TypeScript by ourselves? We certainly can if we have a specific idea in our minds. But for most use cases, there already exist some packages that may suit our needs! There's a lot of them actually, for example &lt;a href="https://github.com/gcanti/io-ts" rel="noopener noreferrer"&gt;io-ts&lt;/a&gt; or &lt;a href="https://github.com/joanllenas/ts.data.json" rel="noopener noreferrer"&gt;ts.data.json&lt;/a&gt; packages. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;: You can find more about the idea of JSON decoders in TypeScript by reading &lt;a href="https://lorefnon.tech/2018/03/25/typescript-and-validations-at-runtime-boundaries/" rel="noopener noreferrer"&gt;Typescript and validations at runtime boundaries&lt;/a&gt; article by &lt;a href="https://twitter.com/lorefnon" rel="noopener noreferrer"&gt;@lorefnon&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take &lt;a href="https://github.com/joanllenas/ts.data.json" rel="noopener noreferrer"&gt;ts.data.json&lt;/a&gt; as an example because it is the smallest one and brings the idea of validation of a JSON string in the simplest way (&lt;a href="https://github.com/gcanti/io-ts" rel="noopener noreferrer"&gt;io-ts&lt;/a&gt; seems to be the most popular one, though). With this package we create our decoder in TypeScript like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;JsonDecoder&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ts.data.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;swFilmDecoder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JsonDecoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SWFilm&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JsonDecoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;episode_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JsonDecoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;opening_crawl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JsonDecoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SWFilm&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can use it in our &lt;code&gt;fetchFilm&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchFilm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filmId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SWFilm&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://swapi.co/api/films/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;filmId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;film&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;swFilmDecoder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decodePromise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;film&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That fits perfectly with our use case! We are now 100% sure that if something wrong will come from the REST API, we can handle it. &lt;code&gt;decodePromise&lt;/code&gt; just returns our film wrapped in a new Promise when parsing succeeds. In case of failure, the decoder would throw an error with a pretty readable message: e.g. &lt;code&gt;&amp;lt;SWFilm&amp;gt; decoder failed at key "opening_crawl" with error: null is not a valid string&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;(Do you see it redundant that we create both interface and the decoder definition that looks basically the same? Yeah, me too 🙁 But don't you worry, some packages, like &lt;a href="https://github.com/gcanti/io-ts" rel="noopener noreferrer"&gt;io-ts&lt;/a&gt;, provide you with a way to define decoder and create a &lt;code&gt;type&lt;/code&gt; definition based on that decoder with only one line! 😎)&lt;/p&gt;

&lt;p&gt;And we're done! With this solution, we don't have to worry about type errors in runtime and are able to handle them gracefully 😉&lt;/p&gt;




&lt;p&gt;This is the end of &lt;strong&gt;part one&lt;/strong&gt; of this mini-series on using Elm to improve your TypeScript! Stay tuned for another article that (hopefully 😛) will come soon!&lt;/p&gt;

</description>
      <category>functional</category>
      <category>elm</category>
      <category>typescript</category>
    </item>
    <item>
      <title>How I started learning Functional Programming</title>
      <dc:creator>Kajetan Świątek</dc:creator>
      <pubDate>Mon, 27 Jan 2020 19:43:12 +0000</pubDate>
      <link>https://dev.to/kajetansw/how-i-started-learning-functional-programming-4k5b</link>
      <guid>https://dev.to/kajetansw/how-i-started-learning-functional-programming-4k5b</guid>
      <description>&lt;p&gt;There was a moment when I declared on my &lt;a href="https://kajetan.dev"&gt;blog&lt;/a&gt;, that I’ll share with you how and why I decided to dive into Functional Programming. And that day is today! I’ll guide you along the path I took to understand basic concepts of FP.&lt;/p&gt;




&lt;h3&gt;
  
  
  The beginning
&lt;/h3&gt;

&lt;p&gt;My first meaningful encounter with FP was during local &lt;a href="https://github.com/WrocTypeScript/past-events/blob/master/2019-03-27.md"&gt;Wrocław TypeScript meetup&lt;/a&gt;. There I got to hear &lt;a href="https://twitter.com/jozflakus"&gt;Józef Flakus&lt;/a&gt; telling us about core concepts of the FP. We then got to the concept of FRP and how it is used in &lt;a href="https://docs.marblejs.com/"&gt;MarbleJS&lt;/a&gt;, the framework for creating server-side apps. In fact, Józef is the creator of this framework. It was really cool to hear his point of view. Those concepts were entirely new to me, because (as it turned out) I had 100% imperative programming mindset.&lt;/p&gt;

&lt;p&gt;The second talk that day was authored by &lt;a href="https://twitter.com/tomasz_ducin?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"&gt;Tomasz Ducin&lt;/a&gt;. He showed us how we can implement functional composition in TypeScript. It was a brilliant live coding session but… I had no idea what he was talking about 😉 I was examining code created by Tomasz for the entire next day until I fully understood what was going on.&lt;/p&gt;

&lt;p&gt;But those talks did it for me. Something clicked and I started diving into FP world step by step for the next couple of months.&lt;/p&gt;




&lt;h3&gt;
  
  
  My first resources
&lt;/h3&gt;

&lt;p&gt;The first thing I did was looking into &lt;a href="https://github.com/sindresorhus/awesome"&gt;Awesome&lt;/a&gt; GitHub repository looking for some basic FP articles or videos. And I was not disappointed. I found two interesting sites: &lt;a href="https://github.com/lucasviola/awesome-functional-programming#readme"&gt;Awesome Functional Programming&lt;/a&gt; and &lt;a href="https://github.com/stoeffel/awesome-fp-js#readme"&gt;Awesome FP JS&lt;/a&gt;. There you can find many reliable resources, but many of them seem really hardcore for people starting with FP, so tread carefully.&lt;/p&gt;

&lt;p&gt;There are also a couple of honorable articles/videos/tutorials that I want to share with you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; &lt;a href="https://www.youtube.com/watch?v=ZhuHCtR3xq8"&gt;“Don’t fear the monad” talk with Brian Beckman&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To this day I remember watching the video with my jaw dropped. The hour just wasn’t enough! Brian is a brilliant man with an extraordinary skill to attract a listener. He can explain difficult concepts with a couple of words and in a fun way. Watching this video just deepened my fascination with the subject.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; &lt;a href="https://www.youtube.com/watch?v=iSmkqocn0oQ"&gt;“Haskell is useless” – talk with Erik Meijer and Simon Peyton Jones&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Talk with two designers of Haskell language on a position of Haskell in the programming world. There we can also get an explanation of the famous diagram of how imperative and declarative languages relate to each other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; &lt;a href="https://soundcloud.com/lambda-cast"&gt;LambdaCast podcast&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It took me a couple of weeks of listening to all the episodes but man, it was so worth it! For around 20 episodes podcast’s hosts and guests explained the core concepts and principles of FP in an approachable way. Just a flawless way to start learning for people with no prior knowledge, like me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; &lt;a href="http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html"&gt;Functors, Applicatives, And Monads In Pictures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another way to approach the problem of learning something new – through visualization. That’s the resource I come back to most frequently due to its simplicity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; &lt;a href="https://vimeo.com/113588389"&gt;“Functional programming design patterns” talk by Scott Wlaschin&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Insightful and fun talk on how FP relates to OOP’s principles and patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; &lt;a href="https://mostly-adequate.gitbooks.io/mostly-adequate-guide/content/#"&gt;“Mostly Adequate Guide to Functional Programming (in JS)” book by Brian Lonsdorf&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is my favorite resource on this list. If you have a JavaScript background (like me) it’s an excellent tutorial. Due to being a multi-paradigm language, it’s super easy to explain FP using JS. I took my time with this book thoroughly re-reading chapters and examining examples. I think nothing else deepened my knowledge of the topic like this book, especially due to many practical examples. I recommend reading it through GitBooks – there you can do all the exercises inside your browser, without cloning the entire repository.&lt;/p&gt;

&lt;p&gt;Those are the most important resources I found to this day about the FP. If you know other interesting resources, don’t hesitate to share them with me and others 😊&lt;/p&gt;

&lt;p&gt;For the last part, I would like to give you some advices, which might just save you time googling 😉&lt;/p&gt;




&lt;h3&gt;
  
  
  Which language to pick to start my journey with FP?
&lt;/h3&gt;

&lt;p&gt;I think there is no bad answer here. You should think about what you would like to do build the language first. If you want to create some web application – maybe try &lt;a href="https://elm-lang.org/"&gt;Elm&lt;/a&gt;. I personally started with Elm and I’m creating some simple recipe app as an exercise. &lt;/p&gt;

&lt;p&gt;If you want to create for the web you might as well use &lt;a href="https://elixir-lang.org/"&gt;Elixir&lt;/a&gt;, which can handle both front-end and back-end with its own Phoenix Framework. If you want to dive deep into the theory of FP you can use &lt;a href="http://www.purescript.org/"&gt;PureScript &lt;/a&gt; (which is my next personal pick) or &lt;a href="https://www.haskell.org/"&gt;Haskell&lt;/a&gt;. (PureScript seems more attractive to me just because it compiles to JS and uses the same ecosystem.) &lt;/p&gt;

&lt;p&gt;If you have JS background and don’t want to learn any new language you can use FP libraries for JavaScript (&lt;a href="https://ramdajs.com/"&gt;Ramda&lt;/a&gt;) or TypeScript (&lt;a href="https://gcanti.github.io/fp-ts/"&gt;fp-ts&lt;/a&gt;). There are also JS frameworks that use FP for building for the web, e.g. MarbleJS (REST API) or CycleJS (web apps).&lt;/p&gt;




&lt;h3&gt;
  
  
  Why should I even be interested in FP?
&lt;/h3&gt;

&lt;p&gt;There are a couple of reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functional means declarative - declare what want to do, let the compiler do the rest. I find FP programs to be much more descriptive than others.&lt;/li&gt;
&lt;li&gt;FP ensures immutability. Once created, variables cannot be changed. With that, you can forget about constantly passing reference to an object and mutating it unknowingly along the way.&lt;/li&gt;
&lt;li&gt;Functions are like building blocks - you can have a lot of them, with any shape you want. Just start building, like with Legos.&lt;/li&gt;
&lt;li&gt;Pure functions help in preventing bugs. Just imagine having no unexpected behavior for your functions – isn’t that neat?&lt;/li&gt;
&lt;li&gt;FP simplifies your unit testing. Having most of your functions pure makes simpler test scenarios and ensures test separation.&lt;/li&gt;
&lt;li&gt;It broadens your mind and lets you try new things. It gives you a new perspective and makes you a more reliable engineer. Switching mindset like that is a great challenge for any developer.&lt;/li&gt;
&lt;li&gt;FP technologies are growing like never before. They’re becoming popular and you can see them more in production applications. Companies are also looking for people with knowledge of FP more often.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  I reviewed all the resources you provided - what should I do next?
&lt;/h3&gt;

&lt;p&gt;Well, I have no idea, I just got there myself when writing this 😉 I’m sure I’ll share with you guys anything interesting I’ll learn.&lt;/p&gt;




&lt;p&gt;⚡️ Originally posted on &lt;a href="https://kajetan.dev"&gt;kajetan.dev&lt;/a&gt; ⚡️&lt;/p&gt;

</description>
      <category>functional</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
