{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/2021-06-15-typescript-basics/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"TypeScript Basics","date":"2021-06-15"},"html":"<p>I have been using JavaScript for quite some time now, and recently I started using TypeScript in my projects. TypeScript is basically JavaScript with types. It was developed by Microsoft and it compiles down to plain JavaScript.</p>\n<h3>Why TypeScript</h3>\n<p>The main reason I started using TypeScript is because it helps catching errors early during development. In JavaScript, we can assign any value to any variable, and we will only find out if something is wrong when we run the app. With TypeScript, we get errors in the editor before we even run the code.</p>\n<p>For example, in JavaScript we can do something like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">let price = 100;\nprice = \"free\";</code></pre></div>\n<p>This is valid JavaScript, but it can cause unexpected behaviour later in the code if we try to do calculations with <code class=\"language-text\">price</code>. In TypeScript, once we declare <code class=\"language-text\">price</code> as a number, we cannot assign a string to it.</p>\n<h3>Basic Types</h3>\n<p>TypeScript has several basic types that we use frequently:</p>\n<p><code class=\"language-text\">string</code> for text values, <code class=\"language-text\">number</code> for numeric values, <code class=\"language-text\">boolean</code> for true or false, <code class=\"language-text\">array</code> for list of values, and <code class=\"language-text\">any</code> which basically means it can be anything, similar to normal JavaScript.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">let name: string = \"David\";\nlet age: number = 25;\nlet isActive: boolean = true;\nlet scores: number[] = [90, 85, 92];</code></pre></div>\n<h3>Interface</h3>\n<p>One of the features that I find very useful is interface. We can define the shape of an object using an interface. This is helpful when we are passing data around, because we know exactly what properties are available and what types they are.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">interface User {\n  id: number;\n  name: string;\n  email: string;\n  isAdmin?: boolean;\n}</code></pre></div>\n<p>The question mark on <code class=\"language-text\">isAdmin</code> means that property is optional.</p>\n<h3>Functions</h3>\n<p>We can also define types for function parameters and return values. This makes it clear what the function expects and what it gives back.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">function calculateTotal(price: number, quantity: number): number {\n  return price * quantity;\n}</code></pre></div>\n<p>If we try to call <code class=\"language-text\">calculateTotal(\"ten\", 5)</code>, TypeScript will show an error immediately.</p>\n<h3>Getting Started</h3>\n<p>To start using TypeScript in a React project, we can use <code class=\"language-text\">npx create-react-app my-app --template typescript</code> or if we already have an existing project, we can install TypeScript and rename our <code class=\"language-text\">.js</code> files to <code class=\"language-text\">.tsx</code> for React components or <code class=\"language-text\">.ts</code> for regular files. It does take some time to get used to, especially when dealing with complex types, but the benefits are worth it. Our code becomes more predictable and easier to maintain.</p>"}},"pageContext":{"slug":"/2021-06-15-typescript-basics/"}},"staticQueryHashes":[]}