<?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: Inya Chukwuemeka</title>
    <description>The latest articles on DEV Community by Inya Chukwuemeka (@benfixit).</description>
    <link>https://dev.to/benfixit</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%2F498429%2Fccafc47d-3a9b-4ad0-9895-70ed603f04c4.jpeg</url>
      <title>DEV Community: Inya Chukwuemeka</title>
      <link>https://dev.to/benfixit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benfixit"/>
    <language>en</language>
    <item>
      <title>Setting up Babel and Typescript</title>
      <dc:creator>Inya Chukwuemeka</dc:creator>
      <pubDate>Sun, 29 Aug 2021 11:00:23 +0000</pubDate>
      <link>https://dev.to/benfixit/setting-up-babel-and-typescript-5ba8</link>
      <guid>https://dev.to/benfixit/setting-up-babel-and-typescript-5ba8</guid>
      <description>&lt;p&gt;Typescript has it's own compiler but there are times when you might want to use a different compiler due to personal preference or project requirements. In this article, we will learn how to use a compiler like Babel to compile our Typescript file to Javascript so that our code can run on any browser whilst the function of the Typescript compiler &lt;code&gt;tsc&lt;/code&gt; will be limited to type checking our code.&lt;/p&gt;

&lt;p&gt;For us to achieve this, we would make use of Babel's typescript preset, &lt;code&gt;@babel/preset-typescript&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Packages
&lt;/h2&gt;

&lt;p&gt;Let's start by setting up our project and adding typescript as a dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn init -y
yarn add typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-y&lt;/code&gt; flag initialises our project and creates a &lt;code&gt;package.json&lt;/code&gt; file with some default values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D @babel/core @babel/cli @babel/preset-env @babel/preset-typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-D&lt;/code&gt; flag lets us add the packages as dev dependencies. Dev dependencies are packages that are only required during development.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@babel/core&lt;/code&gt; - The core functionalities of Babel resides in this module.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@babel/cli&lt;/code&gt; - We need this module so that we can run Babel from the command line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@babel/preset-env&lt;/code&gt; - This contains a collection of plugins that we need to transform our ES2015+ code to backwards compatible Javascript that both current and old browsers and other environments can understand.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@babel/preset-typescript&lt;/code&gt; - This strips out the types from our typescript code and lets Babel compile it as regular javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;p&gt;Next, we need to configure babel and typescript. We will configure Babel so that it can compile our typescript file to Javascript. We will then configure Typescript in such a way that it only checks the typings in our code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Babel
&lt;/h3&gt;

&lt;p&gt;Here, we will tell Babel to use the presets we installed in the previous section to transform our code.&lt;/p&gt;

&lt;p&gt;Create a babel config file &lt;code&gt;babel.config.json&lt;/code&gt; in the root directory of your project and insert the following content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "presets": ["@babel/env", "@babel/preset-typescript"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring Typescript
&lt;/h3&gt;

&lt;p&gt;Here we will tell Typescript to only check the types in our code and not to compile any files. Babel will handle the compilation for us.&lt;/p&gt;

&lt;p&gt;Create a typescript config file &lt;code&gt;tsconfig.json&lt;/code&gt; in the root directory of your project and insert the following content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "compilerOptions": {
        "noEmit": true
     }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing what you've built
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Type checking by TSC
&lt;/h3&gt;

&lt;p&gt;While writing your code, you will get realtime type-checking by tsc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiling with Babel
&lt;/h3&gt;

&lt;p&gt;You can then compile your code with Babel by running the following code on your command line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules/.bin/babel src --out-dir lib --extensions ".ts"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command above tells Babel which we installed to compile every Typescript file in the &lt;code&gt;/src&lt;/code&gt; folder into regular Javascript and output them in the &lt;code&gt;/lib&lt;/code&gt; folder. If your typescript files are located in a different folder, just replace &lt;code&gt;src&lt;/code&gt; with the location of the source files you wish to transform.&lt;/p&gt;

&lt;h3&gt;
  
  
  Auto-compiling with scripts
&lt;/h3&gt;

&lt;p&gt;We can setup some npm scripts to help us watch and compile our files with Babel every time we make changes to them. By doing so, we do not always have to run Babel on the command line ourselves anytime we make a change.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;package.json&lt;/code&gt; file, add the following lines of code in the script tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "watch": "babel src --out-dir lib --extensions '.ts' --watch",
    "build": "babel src --out-dir lib --extensions '.ts'",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can run &lt;code&gt;yarn run watch&lt;/code&gt;, make changes to our code and see the compiled files in the &lt;code&gt;lib&lt;/code&gt; folder get updated when we save our code.&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/benfixit/babel-typescript" rel="noopener noreferrer"&gt;github repository&lt;/a&gt; of the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;This article was written for Typescript 4.x and Babel 7.x. Here are some links you can check for further reading:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://babeljs.io/docs/en/" rel="noopener noreferrer"&gt;Babel documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.typescriptlang.org/docs/" rel="noopener noreferrer"&gt;Typescript documentation&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>babel</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
