Google News
logo
TypeScript Interview Questions
TypeScript is a free and open-source programming language developed and maintained by Microsoft. It is a strongly typed superset of JavaScript that compiles to plain JavaScript. It is a language for application-scale JavaScript development. TypeScript is quite easy to learn and use for developers familiar with C#, Java and all strong typed languages.
 
TypeScript can be executed on Any browser, Any Host, and Any Operating System. TypeScript is not directly run on the browser. It needs a compiler to compile and generate in JavaScript file. TypeScript is the ES6 version of JavaScript with some additional features.

Syntax :
var message:string = "Welcome to Free Time Learning"
console.log(message)
A TypeScript code is written in a file with .ts extension and then compiled into JavaScript using the compiler. You can write the file in any code editor and the compiler needs to be installed on your platform. After the installation, the command tsc <filename>.ts compiles the TypeScript code into a plain JavaScript file.
 
SN JavaScript TypeScript
1 It was developed by Netscape in 1995. It was developed by Anders Hejlsberg in 2012.
2 JavaScript source file is in ".js" extension. TypeScript source file is in ".ts" extension.
3 JavaScript doesn't support ES6. TypeScript supports ES6.
4 It doesn't support strongly typed or static typing. It supports strongly typed or static typing feature.
5 It is just a scripting language. It supports object-oriented programming concept like classes, interfaces, inheritance, generics, etc.
6 JavaScript has no optional parameter feature. TypeScript has optional parameter feature.
7 It is interpreted language that's why it highlighted the errors at runtime. It compiles the code and highlighted errors during the development time.
8 JavaScript doesn't support modules. TypeScript gives support for modules.
9 In this, number, string are the objects. In this, number, string are the interface.
10 JavaScript doesn't support generics. TypeScript supports generics.
There are different reasons why a JavaScript developer should consider using TypeScript. Some of them include :
 
Using new features of ECMAScript : TypeScript supports new ECMAScript standards and transpile them to ECMAScript targets of your choice. So, you can use features of ES2015 and beyond.
 
Static Typing : JavaScript is dynamically typed and does not know what type a variable is until it is actually instantiated at run-time. TypeScript adds type support to JavaScript.
 
Type Inference : TypeScript makes typing a bit easier and a lot less explicit by the usage of type inference. Even if you don’t explicitly type the types, they are still there to save you from doing something which otherwise would result in a run-time error.
 
Better IDE Support : The development experience with TypeScript is a great improvement over JavaScript. There is a wide range of IDEs that have excellent support for TypeScript, like Visual Studio & VS code, Atom, Sublime, and IntelliJ/WebStorm.
 
Strict Null Checking : Errors, like cannot read property ‘x’ of undefined, is common in JavaScript programming. You can avoid most of these kinds of errors since one cannot use a variable that is not known to the TypeScript compiler.
 
Interoperability : TypeScript is closely related to JavaScript so it has great interoperability capabilities, but some extra work is required to work with JavaScript libraries in TypeScript.
Cross-Platform :  The TypeScript compiler can be installed on any Operating System such as Windows, MacOS, and Linux.
 
Object-Oriented Language : TypeScript provides features like Classes, Interfaces, and Modules. Thus, it can write object-oriented code for client-side as well as server-side development.
 
Static Type-Checking : TypeScript uses static typing and helps type checking at compile time. Thus, you can find errors while writing the code without running the script.
 
Optional Static Typing : TypeScript also allows optional static typing in case you are using the dynamic typing of JavaScript.
 
DOM Manipulation : You can use TypeScript to manipulate the DOM for adding or removing elements.
 
ES 6 Features : TypeScript includes most features of planned ECMAScript 2015 (ES 6, 7) such as class, interface, Arrow functions, etc.
TypeScript has the following benefits.
 
* It provides the benefits of optional static typing. Here, Typescript provides types that can be added to variables, functions, properties, etc.
* Typescript has the ability to compile down to a version of JavaScript that runs on all browsers.
* TypeScript always highlights errors at compilation time during the time of development whereas JavaScript points out errors at the runtime.
* TypeScript supports strongly typed or static typing whereas this is not in JavaScript.
* It helps in code structuring.
* It uses class-based object-oriented programming.
* It provides excellent tooling supports with IntelliSense which provides active hints as the code is added.
* It has a namespace concept by defining a module.
TypeScript has the following disadvantages :
 
* TypeScript takes a long time to compile the code.
* TypeScript does not support abstract classes.
* If we run the TypeScript application in the browser, a compilation step is required to transform TypeScript into JavaScript.
* Web developers are using JavaScript from decades and TypeScript doesn?t bring anything new.
* To use any third party library, the definition file is must. And not all the third party library have definition file available.
* Quality of type definition files is a concern.
TypeScript has three primitive types that are frequently used : string, number, and boolean. These correspond to the similarly named types in JavaScript. 
 
* string : represents text values such as “javascript”, “typescript”, etc.
* number : represents numeric values like 1, 2, 32, 43, etc,.
* boolean : represents a variable that can have either a ‘true’ or ‘false’ value.
There are three different types of components in TypeScript which includes :
 
Language : It comprises of the syntax, keywords, and type annotations.
 
The TypeScript Compiler : This compiler (tsc) converts the instructions written in TypeScript to its JavaScript equivalent.
 
The TypeScript Language Service : The Language Service exposes an additional layer around the core compiler pipeline, editor-like applications. The language service supports the common set of typical editor operations.
TypeScript can be installed and managed with the help of node via npm (the Node.js package manager). To install TypeScript, first ensure that the npm is installed correctly, then run the following command which installs TypeScript globally on the system.
$ npm install -g typescript  
It installs a command line code "tsc" which will further be used to compile our Typescript code. Make sure that we check the version of Typescript installed on the system.
 
Following steps are involved for installing TypeScript :
 
* Download and run the .msi installer for the node.
* Enter the command "node -v" to check if the installation was successful.
* Type the following command in the terminal window to install Typescript : $ npm install -g typescript
The extension for any TypeScript file is .ts. And any JavaScript file is a TypeScript file as it is a superset of JavaScript. So, once you change the extension of “.js” to “.ts”, your TypeScript file is ready. To compile any .ts file into .js use the following command :
 
tsc <TypeScript File Name>
 
For example, to compile “freetimelearn.ts
 
tsc freetimelearn.ts
 
And the result would be freetimelearn.js