<?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>Forem: Karimulla Kanugala</title>
    <description>The latest articles on Forem by Karimulla Kanugala (@karimulla_kanugala).</description>
    <link>https://forem.com/karimulla_kanugala</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%2F2882780%2F9ee51042-4727-459b-a455-35618f00e8c6.png</url>
      <title>Forem: Karimulla Kanugala</title>
      <link>https://forem.com/karimulla_kanugala</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/karimulla_kanugala"/>
    <language>en</language>
    <item>
      <title>A Beginner's Guide to TypeScript</title>
      <dc:creator>Karimulla Kanugala</dc:creator>
      <pubDate>Tue, 04 Mar 2025 07:54:36 +0000</pubDate>
      <link>https://forem.com/karimulla_kanugala/a-beginners-guide-to-typescript-279f</link>
      <guid>https://forem.com/karimulla_kanugala/a-beginners-guide-to-typescript-279f</guid>
      <description>&lt;p&gt;Hey there! Have you ever written JavaScript code and run into frustrating bugs that could have been avoided with a little extra help? Well, that’s where TypeScript comes in! Developed by Microsoft, TypeScript is like JavaScript’s smarter, more reliable sibling. It enhances JavaScript by adding static types, making your code cleaner, safer, and more scalable. If you're curious about how it works, stick with me—I promise it'll be worth it!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is TypeScript?
&lt;/h2&gt;

&lt;p&gt;At its core, TypeScript is a superset of JavaScript that compiles down to plain JavaScript. That means everything you know and love about JavaScript still works, but now with added superpowers like type safety. Imagine catching errors before even running your code—sounds like a dream, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use TypeScript?
&lt;/h2&gt;

&lt;p&gt;Let’s talk about why you should consider using TypeScript in your projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Static Typing&lt;/strong&gt; – No more unexpected &lt;code&gt;undefined is not a function&lt;/code&gt; errors. TypeScript helps you catch type-related issues before your code even runs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved Readability&lt;/strong&gt; – Your code becomes more self-documenting, making it easier for you (and your team) to understand.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhanced Tooling&lt;/strong&gt; – Autocomplete, IntelliSense, and error checking in IDEs make coding a breeze.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt; – Whether it's a small script or a massive project, TypeScript helps keep your code organized and maintainable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ESNext Features&lt;/strong&gt; – Get access to modern JavaScript features before they hit mainstream JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Features of TypeScript
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Type Annotations – A Safety Net for Your Code&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let message: string = "Hello, TypeScript!";
console.log(message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This little tweak ensures that &lt;code&gt;message&lt;/code&gt; is always a string—no accidental number assignments!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Interfaces – Blueprint for Objects&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Person {
  name: string;
  age: number;
}

let user: Person = { name: "Alice", age: 25 };
console.log(user.name);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of interfaces as a contract your objects must follow.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Classes and Inheritance – Object-Oriented Goodness&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal {
  constructor(public name: string) {}
  makeSound(): void {
    console.log("Some generic sound");
  }
}

class Dog extends Animal {
  makeSound(): void {
    console.log("Bark!");
  }
}

let myPet = new Dog("Buddy");
myPet.makeSound();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Perfect for structuring your applications with OOP principles.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Generics – Reusability at Its Best&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function identity&amp;lt;T&amp;gt;(arg1:T, arg2: T): T {
  return arg2;
}
console.log(identity&amp;lt;number&amp;gt;(4, 6));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generics allow you to write flexible and reusable functions while keeping type safety.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up TypeScript Project
&lt;/h2&gt;

&lt;p&gt;Ready to dive in? Let’s set up a TypeScript project from scratch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Initialize the Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, create a new project directory and initialize a Node.js project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir typescript
cd typescript
npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Install TypeScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install TypeScript as a development dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installation, you can check the TypeScript version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Initialize TypeScript Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run the following command to generate a &lt;code&gt;tsconfig.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a &lt;code&gt;tsconfig.json&lt;/code&gt; file with default configurations. You can modify it based on your project needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Configure&lt;/strong&gt; &lt;code&gt;tsconfig.json&lt;/code&gt; &lt;strong&gt;(Optional but Recommended)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Modify the file for better project organization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;"target": "ES6"&lt;/code&gt; → Compiles to modern JavaScript.&lt;br&gt;
&lt;code&gt;"module": "CommonJS"&lt;/code&gt; → Ensures compatibility with Node.js.&lt;br&gt;
&lt;code&gt;"outDir": "./dist"&lt;/code&gt; → Compiled files will be placed in the &lt;code&gt;dist&lt;/code&gt; folder.&lt;br&gt;
&lt;code&gt;"rootDir": "./src"&lt;/code&gt; → TypeScript files will be inside the &lt;code&gt;src&lt;/code&gt; folder.&lt;br&gt;
&lt;code&gt;"strict": true"&lt;/code&gt; → Enables strict type checking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Create a &lt;code&gt;src&lt;/code&gt; Directory and a Sample File&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;src&lt;/code&gt; directory and a &lt;code&gt;index.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir src
touch src/index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Compile TypeScript Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run the TypeScript compiler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate a &lt;code&gt;dist/index.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Run the Compiled JavaScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Execute the compiled JavaScript file using Node.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node dist/index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;8. Automate Compilation with tsc --watch (Optional)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For continuous compilation, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --watch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will automatically compile files on changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Add Scripts to package.json (Optional)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Modify &lt;code&gt;package.json&lt;/code&gt; to add convenience scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "build": "tsc",
  "start": "node dist/index.js"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run build&lt;/code&gt; → Compiles TypeScript code.&lt;br&gt;
&lt;code&gt;npm run start&lt;/code&gt; → Runs the compiled JavaScript.&lt;/p&gt;

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

&lt;p&gt;So, what do you think? TypeScript is like JavaScript but with an extra layer of safety and clarity. Whether you’re working on a small script or a large-scale application, TypeScript can make your life a whole lot easier.&lt;/p&gt;

&lt;p&gt;If you haven’t tried TypeScript yet, now is the perfect time to give it a shot. Trust me, once you start using it, you won’t want to go back!&lt;/p&gt;

&lt;p&gt;Got any questions or thoughts? Drop them in the comments—I’d love to hear from you!&lt;/p&gt;

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