{"id":11214,"date":"2020-12-17T09:47:35","date_gmt":"2020-12-17T14:47:35","guid":{"rendered":"https:\/\/www.carnaghan.com\/?p=11214"},"modified":"2020-12-17T15:35:08","modified_gmt":"2020-12-17T20:35:08","slug":"typescript-demystified","status":"publish","type":"post","link":"https:\/\/www.carnaghan.com\/typescript-demystified\/","title":{"rendered":"TypeScript Demystified"},"content":{"rendered":"\n

TypeScript can be a confusing term for anyone new to JavaScript development. The first impression of TypeScript for beginners is that it is a programming language, more or less, similar to JavaScript. Even professionals with enough coding experience often fail to define TypeScript. So how can we put the answer to this question – What is TypeScript?<\/p>\n\n\n\n

TypeScript is a superset of JavaScript. It has optional static typing, classes, and interfaces. TypeScript is not a separate programming language, what makes it different from JavaScript lies in part of its name – Type<\/em>. Every programming language has data types, In JavaScript these include the commonly used number, string, boolean, etc. To understand TypeScript, we first need to understand how JavaScript behaves with its data types.<\/p>\n\n\n\n

Weak and Strongly typed Languages<\/h2>\n\n\n\n

JavaScript is a dynamically typed language, which means the type of variables defined is checked at runtime. When we write a JavaScript code and declare variables, we don’t define any data type, we simply declare a variable.<\/p>\n\n\n\n

a = 1234\nb = \"Hello\"\nc = true<\/code><\/pre>\n\n\n\n

In JavaScript, we can declare a variable and assign a value to it, and then later we can assign another value to different data types. We can keep on doing this and it is possible because JavaScript is a dynamically-typed language.<\/p>\n\n\n\n

let a = 100\na = \"Hello\"\na = 1234<\/code><\/pre>\n\n\n\n

The above code will work fine. The first value assigned to the variable “a” is a number and then a string is assigned to it. After that, again a Number is assigned to it. This may be not possible in other languages that are statically (or strongly) typed.<\/p>\n\n\n\n

Due to the nature of being dynamically-typed, JavaScript is also a weak-typed language. By weak-typed, it means we can do anything in the expression.<\/p>\n\n\n\n

 let a = \"100\"\n let b = 200\n let c = a + b <\/code><\/pre>\n\n\n\n

“a” holds a string while b holds a Number. Yet, when both of these are added and assigned to “c”, it works fine. Something like this may not be possible in other programming languages. A weak-typed language has relaxed and lenient rules while it’s opposite, a strong-typed language has more strict rules.<\/p>\n\n\n\n

Features of TypeScript<\/h2>\n\n\n\n

1. Object-oriented<\/h3>\n\n\n\n

JavaScript has traditionally not supported object-oriented programming features, however in later versions it has improved. TypeScript supports object-oriented programming out of the box. It comes with features such as classes, interfaces, modules, and more.<\/p>\n\n\n\n

2. JavaScript is TypeScript<\/h3>\n\n\n\n

Apart from differences, both JavaScript and TypeScript have many similarities. Actually, JavaScript is TypeScript. Any file with .js extension (JavaScript file) can be converted into a TypeScript .ts file. <\/p>\n\n\n\n

3. Support for JavaScript libraries<\/h3>\n\n\n\n

As mentioned above, JavaScript is TypeScript. So each element of JavaScript is supported in TypeScript. All JavaScript libraries, frameworks, modules, and existing code work with TypeScript.<\/p>\n\n\n\n

4. DOM manipulation<\/h3>\n\n\n\n

Like JavaScript, DOM manipulation is also possible with TypeScript.<\/p>\n\n\n\n

5. Portable<\/h3>\n\n\n\n

TypeScript is extremely portable. It can run on any browser, operating system, or device. TypeScript can run in any environment where JavaScript can.<\/p>\n\n\n\n

Working with TypeScript<\/h2>\n\n\n\n

To work with TypeScript, we first have to install it. <\/p>\n\n\n\n

TypeScript can be installed using the Node Package Manager. Make sure Node.js and NPM installed. Open a terminal and run the following command.<\/p>\n\n\n\n

npm install -g typescript<\/code><\/pre>\n\n\n\n

The command will install TypeScript globally. Now we can create our first TypeScript program.<\/p>\n\n\n\n

Create a new file and name it example.ts. While creating a JavaScript file, we use .js extension. In the case of the TypeScript file, we use .ts. Next, paste the following line of code into the newly created .ts file.<\/p>\n\n\n\n

console.log(\"Hello World!\")<\/code><\/pre>\n\n\n\n

To run the TypeScript file, run the following command.<\/p>\n\n\n\n

tsc example.ts<\/code><\/pre>\n\n\n\n

This command will not actually run the file, but convert it into a JavaScript file. You will find a JavaScript file of the same name in the same directory. Now use the basic node command to run the JavaScript file.<\/p>\n\n\n\n

node example.ts<\/code><\/pre>\n\n\n\n
\"\"<\/figure>\n\n\n\n

Next, create variables with different data types in TypeScript.<\/p>\n\n\n\n

 var firstName:string = \"Johnny\"\n var lastName:string = \"Powers\"\n var age:number = 28\n var isPresent:boolean = true\n console.log(\"Name: \" + firstName + \" \" + lastName)\n console.log(\"Age: \" + age)\n console.log(\"Presence: \" + isPresent) <\/code><\/pre>\n\n\n\n

You should notice the first major difference and benefit of TypeScript over JavaScript. TypeScript is a strongly typed language and therefore we declare variables using data types as shown in the example below.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Another major feature of TypeScript is its support for object-oriented programming. Let’s walk through an example:<\/p>\n\n\n\n

class Details {\n\u200b  firstName: string\n\u200b  lastName: string\n\u200b  age: number\n  \u200bisPresent: boolean\n  constructor(firstName: string, lastName: string, age: number, isPresent: boolean) {\n     \u200b\u200bthis.firstName = firstName\n     \u200b\u200bthis.lastName = lastName\n     \u200b\u200bthis.age = age\n\u200b\u200b     this.isPresent = isPresent\n  }\n \u200b\n  \u200bdisplayDetails(){\n    \u200b\u200bconsole.log(\"Name: \" + this.firstName + \" \" + this.lastName)\n\u200b\u200b    console.log(\"Age: \" + this.age)\n    \u200b\u200bconsole.log(\"Presence: \" + this.isPresent)\n\u200b  }\n}\n\nlet d1 = new Details(\"Johnny\", \"Powers\", 28, true);\nlet d2 = new Details(\"Mark\", \"Williams\", 24, false);\n\nd1.displayDetails()\nd2.displayDetails() <\/code><\/pre>\n\n\n\n

In the above TypeScript code, we declared a class named “Details”. This class has four variables with data types, a constructor, and a method named “displayDetails”. It has two different objects (d1 and d2) and these objects are used to initialize. In the end, the method “displayDetails” is called using the two objects.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Advantages and disadvantages of TypeScript<\/h2>\n\n\n\n

Hopefully by now you will have an understanding of the advantages of using TypeScript. It checks error during compile time while JavaScript checks during runtime. Because of this, solving errors becomes easy in TypeScript. As we discussed earlier, TypeScript is strong-typed whereas JavaScript is not. TypeScript supports class and modules and has great tooling support. Some of the disadvantages or drawbacks of TypeScript include the compilation time, which can be high. In addition to this, while running TypeScript in a browser, it is required to compile TypeScript into JavaScript first.<\/p>\n\n\n\n

Conclusion<\/h2>\n\n\n\n

TypeScript is not that complicated as it may seem. It is nothing more than a super-set of JavaScript with several new features such as types and classes. In this article, we discussed what is TypeScript and how it is different from JavaScript. The main point is that JavaScript is a weak-typed language while TypeScript has strict rules. I feel it is definitely work learning and exploring in your own projects to determine the real value it can bring. TypeScript is popular and commonly used in the development community. Finally, JavaScript has flaws thatTypeScript can help to avoid. Professionals who work with TypeScript tend to highly advocate for using it in any work that involves JavaScript development. To learn more about TypeScript, be sure to visit the official site at typescriptlang.org<\/a>, where you will find plenty of resources for documentation, help, and a sandbox for trying it out.<\/p>\n","protected":false},"excerpt":{"rendered":"

TypeScript can be a confusing term for anyone new to JavaScript development. The first impression of TypeScript for beginners is that it is a programming language, more or less, similar to JavaScript. Even professionals with enough coding experience often fail to define TypeScript. So how can we put the answer to this question – What […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"post_series":[],"yoast_head":"\nTypeScript Demystified - Ian Carnaghan<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.carnaghan.com\/typescript-demystified\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TypeScript Demystified - Ian Carnaghan\" \/>\n<meta property=\"og:description\" content=\"TypeScript can be a confusing term for anyone new to JavaScript development. The first impression of TypeScript for beginners is that it is a programming language, more or less, similar to JavaScript. Even professionals with enough coding experience often fail to define TypeScript. So how can we put the answer to this question – What […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.carnaghan.com\/typescript-demystified\/\" \/>\n<meta property=\"og:site_name\" content=\"Ian Carnaghan\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-17T14:47:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-17T20:35:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png\" \/>\n<meta name=\"author\" content=\"Ian Carnaghan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@icarnaghan\" \/>\n<meta name=\"twitter:site\" content=\"@icarnaghan\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ian Carnaghan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/\"},\"author\":{\"name\":\"Ian Carnaghan\",\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\"},\"headline\":\"TypeScript Demystified\",\"datePublished\":\"2020-12-17T14:47:35+00:00\",\"dateModified\":\"2020-12-17T20:35:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/\"},\"wordCount\":968,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\"},\"image\":{\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png\",\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.carnaghan.com\/typescript-demystified\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/\",\"url\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/\",\"name\":\"TypeScript Demystified - Ian Carnaghan\",\"isPartOf\":{\"@id\":\"https:\/\/www.carnaghan.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png\",\"datePublished\":\"2020-12-17T14:47:35+00:00\",\"dateModified\":\"2020-12-17T20:35:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.carnaghan.com\/typescript-demystified\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage\",\"url\":\"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png\",\"contentUrl\":\"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png\",\"width\":885,\"height\":106},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.carnaghan.com\/typescript-demystified\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.carnaghan.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"TypeScript Demystified\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.carnaghan.com\/#website\",\"url\":\"https:\/\/www.carnaghan.com\/\",\"name\":\"Ian Carnaghan\",\"description\":\"Software Developer, Blogger, Educator\",\"publisher\":{\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.carnaghan.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5\",\"name\":\"Ian Carnaghan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g\",\"caption\":\"Ian Carnaghan\"},\"logo\":{\"@id\":\"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/\"},\"description\":\"I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.\",\"sameAs\":[\"http:\/\/www.carnaghan.com\",\"https:\/\/x.com\/icarnaghan\"],\"url\":\"https:\/\/www.carnaghan.com\/author\/icarnaghan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"TypeScript Demystified - Ian Carnaghan","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.carnaghan.com\/typescript-demystified\/","og_locale":"en_US","og_type":"article","og_title":"TypeScript Demystified - Ian Carnaghan","og_description":"TypeScript can be a confusing term for anyone new to JavaScript development. The first impression of TypeScript for beginners is that it is a programming language, more or less, similar to JavaScript. Even professionals with enough coding experience often fail to define TypeScript. So how can we put the answer to this question – What […]","og_url":"https:\/\/www.carnaghan.com\/typescript-demystified\/","og_site_name":"Ian Carnaghan","article_published_time":"2020-12-17T14:47:35+00:00","article_modified_time":"2020-12-17T20:35:08+00:00","og_image":[{"url":"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png"}],"author":"Ian Carnaghan","twitter_card":"summary_large_image","twitter_creator":"@icarnaghan","twitter_site":"@icarnaghan","twitter_misc":{"Written by":"Ian Carnaghan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/#article","isPartOf":{"@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/"},"author":{"name":"Ian Carnaghan","@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5"},"headline":"TypeScript Demystified","datePublished":"2020-12-17T14:47:35+00:00","dateModified":"2020-12-17T20:35:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/"},"wordCount":968,"commentCount":0,"publisher":{"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5"},"image":{"@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage"},"thumbnailUrl":"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png","articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.carnaghan.com\/typescript-demystified\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/","url":"https:\/\/www.carnaghan.com\/typescript-demystified\/","name":"TypeScript Demystified - Ian Carnaghan","isPartOf":{"@id":"https:\/\/www.carnaghan.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage"},"image":{"@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage"},"thumbnailUrl":"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png","datePublished":"2020-12-17T14:47:35+00:00","dateModified":"2020-12-17T20:35:08+00:00","breadcrumb":{"@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.carnaghan.com\/typescript-demystified\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/#primaryimage","url":"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png","contentUrl":"https:\/\/www.carnaghan.com\/wp-content\/uploads\/2020\/12\/ts1.png","width":885,"height":106},{"@type":"BreadcrumbList","@id":"https:\/\/www.carnaghan.com\/typescript-demystified\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.carnaghan.com\/"},{"@type":"ListItem","position":2,"name":"TypeScript Demystified"}]},{"@type":"WebSite","@id":"https:\/\/www.carnaghan.com\/#website","url":"https:\/\/www.carnaghan.com\/","name":"Ian Carnaghan","description":"Software Developer, Blogger, Educator","publisher":{"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.carnaghan.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/c689c24d516c51968a88b628860740a5","name":"Ian Carnaghan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f2aa5baca80c2be728de43a975185d91?s=96&d=retro&r=g","caption":"Ian Carnaghan"},"logo":{"@id":"https:\/\/www.carnaghan.com\/#\/schema\/person\/image\/"},"description":"I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.","sameAs":["http:\/\/www.carnaghan.com","https:\/\/x.com\/icarnaghan"],"url":"https:\/\/www.carnaghan.com\/author\/icarnaghan\/"}]}},"views":938,"_links":{"self":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/posts\/11214"}],"collection":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/comments?post=11214"}],"version-history":[{"count":0,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/posts\/11214\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/media?parent=11214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/categories?post=11214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/tags?post=11214"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/www.carnaghan.com\/wp-json\/wp\/v2\/post_series?post=11214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}