TypeScript Interview Questions


Q#1. What is TypeScript? Why should we use it?

Ans. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript which runs on any browser or JavaScript engine.
TypeScript offers support for the latest JavaScript features and also has some additional features like static typing, object oriented programming and automatic assignment of constructor.


Q#2. Why do we need TypeScript?

Ans. We need TypeScript:

TypeScript is fast, simple, and most importantly, easy to learn.
TypeScript supports object-oriented programming features such as classes, interfaces, inheritance, generics, etc.
TypeScript provides the error-checking feature at compilation time. It will compile the code, and if any error found, then it highlighted the errors before the script is run.
TypeScript supports all JavaScript libraries because it is the superset of JavaScript.
TypeScript support reusability by using the inheritance.
TypeScript make app development quick and easy as possible, and the tooling support of TypeScript gives us autocompletion, type checking, and source documentation.
TypeScript supports the latest JavaScript features including ECMAScript 2015.
TypeScript gives all the benefits of ES6 plus more productivity.
TypeScript supports Static typing, Strongly type, Modules, Optional Parameters, etc.


Q#3. What are Types in TypeScript?

Ans. The type represents the type of the value we are using in our programs. TypeScript supports simplest units of data such as numbers, strings, boolean as well as additional types like enum, any, never.
In TypeScript, we are declaring a variable with its type explicitly by appending the : with the variable name followed by the type.

The reason adding types are:
Types have proven ability to enhance code quality and understandability.
It’s better for the compiler to catch errors than to have things fail at runtime.
Types are one of the best forms of documentation we can have.


Q#4. What is namespace in Typescript? How to declare a namespace in Typescript?

Ans. Internal Modules are known as namespaces in Typescript.Namespaces are used to maintain the legacy code of typescript interally. A namespace is simply a way to logically group related classes or interfaces in a wrapper.
Synatax for creating namespace in Typescript
namespace YourNameSpace { 
   export class Class1 {  }  
   export class Class2 {  }  


Q#5. What are the disadvantages of TypeScript?

Ans. 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 as for how can you be sure the definitions are correct?


Q#6. What is as syntax in TypeScript?

Ans. The as is additional syntax for Type assertion in TypeScript. The reason for introducing the as-syntax is that the original syntax (<type>) conflicted with JSX.


Q#7. Who developed Typescript and what is the current stable version of Typescript?

Ans. The typescript was developed by Anders Hejlsberg, who is also one of the core members of the development team of C# language. The typescript was first released in the month of October 1st, 2012 and was labeled version 0.8. It is developed and maintained by Microsoft under the Apache 2 license. It was designed for the development of a large application.

The current stable version of TypeScript is 3.2 which was released on September 30, 2018. Typescript compiles to simple JavaScript code which runs on any browser that supports ECMAScript 2015 framework. It offers support for the latest and evolving JavaScript features.


Q#8. What is Compilation Context?

Ans. The compilation context is basically grouping of the files that TypeScript will parse and analyze to determine what is valid and what isn’t. Along with the information about which files, the compilation context contains information about which compiler options. A great way to define this logical grouping is using a tsconfig.json file.


Q#9. Is Native Javascript supports modules?

Ans. No. Currently, modules are not supported by Native JavaScript. To create and work with modules in Javascript we require an external like CommonJS.


Q#10. What are all the other access modifiers that TypeScript supports?

Ans. TypeScript supports access modifiers public, private and protected which determine the accessibility of a class member as given below:
public - All the members of the class, its child classes, and the instance of the class can access.
protected - All the members of the class and its child classes can access them. But the instance of the class can not access.
private - Only the members of the class can access them.
If an access modifier is not specified it is implicitly public as that matches the convenient nature of JavaScript.
Also note that at runtime (in the generated JS) these have no significance but will give you compile time errors if you use them incorrectly.