{"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 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 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 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 “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 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 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 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 Like JavaScript, DOM manipulation is also possible with TypeScript.<\/p>\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 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 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 To run the TypeScript file, run the following command.<\/p>\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\nWeak and Strongly typed Languages<\/h2>\n\n\n\n
a = 1234\nb = \"Hello\"\nc = true<\/code><\/pre>\n\n\n\n
let a = 100\na = \"Hello\"\na = 1234<\/code><\/pre>\n\n\n\n
let a = \"100\"\n let b = 200\n let c = a + b <\/code><\/pre>\n\n\n\n
Features of TypeScript<\/h2>\n\n\n\n
1. Object-oriented<\/h3>\n\n\n\n
2. JavaScript is TypeScript<\/h3>\n\n\n\n
3. Support for JavaScript libraries<\/h3>\n\n\n\n
4. DOM manipulation<\/h3>\n\n\n\n
5. Portable<\/h3>\n\n\n\n
Working with TypeScript<\/h2>\n\n\n\n
npm install -g typescript<\/code><\/pre>\n\n\n\n
console.log(\"Hello World!\")<\/code><\/pre>\n\n\n\n
tsc example.ts<\/code><\/pre>\n\n\n\n
node example.ts<\/code><\/pre>\n\n\n\n