<?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: Ankit Bhusal</title>
    <description>The latest articles on DEV Community by Ankit Bhusal (@itsankitbhusal).</description>
    <link>https://dev.to/itsankitbhusal</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F958396%2Fb1449f71-8e96-43e8-8738-adc3ce3963e4.png</url>
      <title>DEV Community: Ankit Bhusal</title>
      <link>https://dev.to/itsankitbhusal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itsankitbhusal"/>
    <language>en</language>
    <item>
      <title>TypeScript Beginning</title>
      <dc:creator>Ankit Bhusal</dc:creator>
      <pubDate>Fri, 28 Oct 2022 16:20:03 +0000</pubDate>
      <link>https://dev.to/itsankitbhusal/typescript-beginning-3pf8</link>
      <guid>https://dev.to/itsankitbhusal/typescript-beginning-3pf8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;TypeScript is &lt;strong&gt;JavaScript&lt;/strong&gt; with syntax for types.&lt;/p&gt;

&lt;p&gt;TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.&lt;/p&gt;

&lt;p&gt;Official website: &lt;a href="https://www.typescriptlang.org/"&gt;TypeScriptLang&lt;/a&gt;&lt;br&gt;
First Release: Oct 2012&lt;br&gt;
Creator: Microsoft ( &lt;strong&gt;Anders Hejlsberg&lt;/strong&gt; creator of C# )&lt;/p&gt;


&lt;h2&gt;
  
  
  Prerequisits
&lt;/h2&gt;

&lt;p&gt;In order to learn &lt;strong&gt;TypeScript&lt;/strong&gt; efficitively you need to have &lt;strong&gt;basic&lt;/strong&gt; knowledge of &lt;strong&gt;JavaScript&lt;/strong&gt; and its &lt;strong&gt;package managers&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScriptm Basics&lt;/li&gt;
&lt;li&gt;Node js installed&lt;/li&gt;
&lt;li&gt;JavaScript Package Manager&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Installation and basics
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; can be installed using &lt;strong&gt;JavaScript&lt;/strong&gt; package managers, i.e, &lt;strong&gt;npm or yarn&lt;/strong&gt; and &lt;strong&gt;-g&lt;/strong&gt; flag to install it globally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    npm &lt;span class="nb"&gt;install &lt;/span&gt;typescript &lt;span class="nt"&gt;-g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For compiling the TypeScript file to JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    tsc &amp;lt;filemane&amp;gt;.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want your JavaScript filename different from your TypeScript file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    tsc &amp;lt;filemane&amp;gt;.ts &amp;lt;outputFileName&amp;gt;.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want your TypeScript file to auto recompile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    tsc &amp;lt;filename&amp;gt;.ts &lt;span class="nt"&gt;-w&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding tsconfig.json
&lt;/h2&gt;

&lt;p&gt;For initialization of &lt;code&gt;tsconfig.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    tsc &lt;span class="nt"&gt;--init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the tsconfig.json file appears in your root &lt;code&gt;/&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Firstly we need to specify the &lt;code&gt;root&lt;/code&gt; directory for our project which is commented by default.&lt;br&gt;
Search for &lt;code&gt;rootDir&lt;/code&gt; in &lt;code&gt;tsconfig.json&lt;/code&gt;, uncomment it and specify the directory where your &lt;strong&gt;TypeScript&lt;/strong&gt; files to be located. In my case, I will be creating all TypeScript files inside &lt;code&gt;src&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;settings&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"rootDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./src"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we also need compiled output of &lt;strong&gt;TypeScript&lt;/strong&gt; file as &lt;strong&gt;JavaScript&lt;/strong&gt; file, for that we need to specify output directory in &lt;code&gt;tsconfig.json&lt;/code&gt;&lt;br&gt;
Search for &lt;code&gt;outDir&lt;/code&gt; in &lt;code&gt;tsconfig.json&lt;/code&gt;, uncomment it and specify your output directory after compilation. In my case, I have made &lt;code&gt;build&lt;/code&gt; directory and &lt;code&gt;js&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;settings&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"outDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./build/js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want your &lt;strong&gt;TypeScript&lt;/strong&gt; files to be compiled inside specific directory i.e, &lt;code&gt;/src&lt;/code&gt; you need to add some setting in your &lt;code&gt;tsconfig.json&lt;/code&gt; file in the end, and final &lt;code&gt;tsconfig.json&lt;/code&gt; looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//all&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;settings&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"include"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"src"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After all these configurations in our &lt;code&gt;tsconfig.json&lt;/code&gt; we should make sure we include all &lt;strong&gt;TypeScript&lt;/strong&gt; files inside &lt;code&gt;/src&lt;/code&gt; directory and look for compiled files in &lt;code&gt;/build/js&lt;/code&gt; according to our configuration in our &lt;code&gt;tsconfig.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For constantly monitoring for change in our &lt;code&gt;/src&lt;/code&gt; directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    tsc &lt;span class="nt"&gt;-w&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Till the terminal is closed or killed, &lt;strong&gt;TypeScript&lt;/strong&gt; will constantly monitor the changes in &lt;code&gt;/src&lt;/code&gt; directory and produce output inside &lt;code&gt;/build/js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;My folder structure after all these settings and a file&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Project&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;build&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;js&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;main.js&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;index.html&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;main.ts&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tsconfig.json&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another killer &lt;strong&gt;TypeScript&lt;/strong&gt; feature is it can compile your &lt;code&gt;.ts&lt;/code&gt; file into any version of &lt;strong&gt;JavaScript&lt;/strong&gt; &lt;code&gt;.js&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;settings&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"es2016"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, &lt;code&gt;target&lt;/code&gt; is set to &lt;code&gt;es2016&lt;/code&gt; but you can compile to any version of &lt;strong&gt;JavaScript&lt;/strong&gt;, i.e, &lt;code&gt;'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'es2022',...&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you remove &lt;code&gt;.ts&lt;/code&gt; file from &lt;code&gt;/src&lt;/code&gt; then the compiled file in &lt;code&gt;/build/js&lt;/code&gt; should be removed manually.&lt;/p&gt;

&lt;p&gt;To prevent auto compiling of the TypeScript file with error, for which we are using &lt;strong&gt;TypeScript&lt;/strong&gt;, we need to enable &lt;code&gt;noEmitOnError&lt;/code&gt; feature in &lt;code&gt;tsconfig.json&lt;/code&gt; by uncommenting that specific line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;settings&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;here&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"noEmitOnError"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now only valid &lt;strong&gt;TypeScript&lt;/strong&gt; files will be compiled to &lt;strong&gt;JavaScript&lt;/strong&gt; file.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>ts</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
