Q1. What is TypeScript and Why Do We Need It?
Ans. JavaScript is the only client side language universally supported by all browsers. But JavaScript is not the best designed language. It’s not a class-based object-oriented language, doesn’t support class based inheritance, unreliable dynamic typing and lacks in compile time error checking. And TypeScript addresses all these problems. In other words, TypeScript is an attempt to “fix” JavaScript problems.
TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language. TypeScript is quite easy to learn and use for developers familiar with C#, Java and all strong typed languages. At the end of day “TypeScript is a language that generates plain JavaScript files.”
As stated on Typescript official website, “TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open Source.” Where “typed” means that it considers the types of variables, parameters and functions.
Q2. What are the Benefits of TypeScript?
Ans. TypeScript has following benefits:
- It helps in code structuring
- Use class based object oriented programming
- Impose coding guidelines
- Offers type checking
- Compile time error checking
- Intellisense
Q3. What are Different Components of TypeScript?
Ans. There are mainly 3 components of TypeScript:
- Language– The most important part for developers is the new language. The language consists of new syntax, keywords and allows you to write TypeScript.
- Compiler– The TypeScript compiler is open source, cross-platform and open specification, and is written in TypeScript. Compiler will compile your TypeScript into JavaScript. And it will also emit error, if any. It can also help in concatenating different files to a single output file and in generating source maps.
- Language Service– TypeScript language service which powers the interactive TypeScript experience in Visual Studio, VS Code, Sublime, the TypeScript playground and other editor.
Q4. How Can You Get TypeScript and Install It?
Ans. TypeScript can be installed and managed via npm, the Node.js package manager. To install TypeScript, first ensure the npm is installed properly. And then run the following command to install TypeScript globally on your system.
Hide Copy Code
npm install -g typescript
TypeScript is included in Visual Studio 2013 Update 2 and Visual Studio 2015 by default. TypeScript also provides support for other editors like Visual Studio Code, sublime, Emacs and Vim.
Q5. How Do You Compile TypeScript Files?
Ans. The extension for any TypeScript file is “.ts”. And any JavaScript file is TypeScript file as it is a superset of JavaScript. So change extension of “.js” to “.ts” file and your TypeScript file is ready. To compile any .ts file into .js, use the following command.
Hide Copy Code
tsc <TypeScript File Name>
For example, to compile “Helloworld.ts”:
Hide Copy Code
tsc helloworld.ts
And the result would be helloworld.js.