<?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: Kelly Xiong Chen </title>
    <description>The latest articles on DEV Community by Kelly Xiong Chen  (@kellyxc).</description>
    <link>https://dev.to/kellyxc</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%2F2493294%2Fcc540c07-de27-4954-a3dd-30cf83a2032f.png</url>
      <title>DEV Community: Kelly Xiong Chen </title>
      <link>https://dev.to/kellyxc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kellyxc"/>
    <language>en</language>
    <item>
      <title>Adding to the Script with TypeScript</title>
      <dc:creator>Kelly Xiong Chen </dc:creator>
      <pubDate>Tue, 07 Jan 2025 21:24:25 +0000</pubDate>
      <link>https://dev.to/kellyxc/adding-to-the-script-with-typescript-5ani</link>
      <guid>https://dev.to/kellyxc/adding-to-the-script-with-typescript-5ani</guid>
      <description>&lt;p&gt;&lt;em&gt;Cover image credit:&lt;/em&gt; &lt;a href="https://unsplash.com/photos/black-typewriter-d34DtRp1bqo" rel="noopener noreferrer"&gt;Daria Kraplak&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;p&gt;Introduction &lt;br&gt;
Purpose of TypeScript &lt;br&gt;
Comparing JavaScript and TypeScript &lt;br&gt;
Conclusion &lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Before diving into what TypeScript is, let’s talk about JavaScript first! Did you know that JavaScript was invented in 1995? I didn’t either. &lt;/p&gt;

&lt;p&gt;During this time, it fulfilled the need of processing simple web pages in people’s browsers. Since 1999, JavaScript has been improved and can handle the fun and dynamic web pages we are building nowadays. In addition, it is a fairly easy-to-use programming language as it is flexible with (slight spoiler here) data types. &lt;/p&gt;

&lt;p&gt;However, there was an issue that was not being addressed. Ironically, the reason why JavaScript is easy to use is the same reason why it can break our code. Here is where TypeScript can help us! &lt;/p&gt;
&lt;h2&gt;
  
  
  Purpose of TypeScript
&lt;/h2&gt;

&lt;p&gt;TypeScript is a superset of JavaScript. What does this mean? Well, to some, TypeScript is not a programming language as it is utilized to assist with the pre-existing JavaScript code. Regardless, TypeScript helps us JavaScript users by reinforcing the structure of our code. This is done by ensuring the consistency in data types for each variable we have. This is beneficial when it comes to testing because it highlights potential bugs during development and warns us before the code is compiled into JavaScript.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Comparing JavaScript and TypeScript
&lt;/h2&gt;
&lt;h3&gt;
  
  
  They Share These Data Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;string&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;number&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, their implementation requires a different approach. Let me show you how! &lt;/p&gt;
&lt;h3&gt;
  
  
  Inferences
&lt;/h3&gt;

&lt;p&gt;When defining a variable in JavaScript, its data type can change later on if we want it to. This is a different story for TypeScript as a variable’s data type is either inferred by TypeScript or explicitly stated by us. A variable’s data type can become &lt;code&gt;any&lt;/code&gt; if it cannot be inferred or it is not explicitly stated!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// JavaScript &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// x stores a number value.  &lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// x is now storing a string value.  &lt;/span&gt;
&lt;span class="c1"&gt;// This is valid since JavaScript allows dynamic type reassignment.  &lt;/span&gt;

&lt;span class="c1"&gt;// TypeScript &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// x stores a number value.  &lt;/span&gt;
&lt;span class="c1"&gt;// (For line below) Error: Type string is not assignable to type number.  &lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="c1"&gt;// The reassigned value below matches the inferred or declared type of &lt;/span&gt;
&lt;span class="c1"&gt;// the variable. TypeScript is okay with this.  &lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  

&lt;span class="c1"&gt;// For both &lt;/span&gt;
&lt;span class="c1"&gt;// x stores a number value and does not allow reassignment here. &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// Error: Cannot assign to 'x' because it is a constant.  &lt;/span&gt;
&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Type Annotation
&lt;/h3&gt;

&lt;p&gt;As a continuation of the subsection above, another way we can explicitly state a variable’s data type in TypeScript is by using type annotation. This means adding a colon and type (&lt;code&gt;: &amp;lt;type&amp;gt;&lt;/code&gt;) after giving a variable its name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// JavaScript &lt;/span&gt;
&lt;span class="c1"&gt;// No need to state the data type here, just assign the variable a value. &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;purple&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Since JavaScript is flexible, it is fine with this reassignment. &lt;/span&gt;
&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;blue&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// This is also the case even if the value changes to 10. &lt;/span&gt;

&lt;span class="c1"&gt;// TypeScript &lt;/span&gt;
&lt;span class="c1"&gt;// 'color' is declared and expected to have a string value.  &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Similar to the line above, except we assign a value as well.  &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;purple&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 'purple' is a string value, so this is valid.  &lt;/span&gt;
&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;blue&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 'blue' is a string value, so this is valid.  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Shapes
&lt;/h3&gt;

&lt;p&gt;What does “shapes” mean in TypeScript? For example, when it comes to &lt;code&gt;strings&lt;/code&gt;, their methods come with their own properties. These “properties” make up the “shape” or structure of the variable. To access a variable’s properties, we must first ensure that the method we are using provides the necessary information. JavaScript is not strict with this and allows variables to change their shapes when it comes to runtime. However, we have to be careful with TypeScript as it is searching for the exact properties of a variable. It is like fitting blocks into the outlines that match their shapes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// JavaScript &lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Result: 5 &lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Result: "THERE"&lt;/span&gt;

&lt;span class="c1"&gt;// TypeScript &lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Result: 5 &lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUppercase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  
&lt;span class="c1"&gt;// Property 'toUppercase' does not exist on type '"there"'.  &lt;/span&gt;
&lt;span class="c1"&gt;// Did you mean 'toUpperCase'? &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are plenty more than these! &lt;/p&gt;

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

&lt;p&gt;These were my first impressions of TypeScript thus far. It is safe to say that I am fascinated by how it can elevate what JavaScript can do for us already. I am eager to learn more and implement it into my JavaScript projects! &lt;/p&gt;

&lt;p&gt;I hope this has piqued your interest in adding TypeScript to your sandbox/toolbox alongside JavaScript. Who wouldn’t want their code to be better structured?&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>programming</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
