Typescript Interview Questions For Freshers & Experienced

  • Question 1) What is Typescript?
  • Question 2) What are modules in TypeScript?
  • Question 3) What are classes in TypeScript?
  • Question 4) What is static typing in Typescript?
  • Question 5) What is use of tsconfig.json file in Typescript?
  • Question 6) What are generics in TypeScript?
  • Question 7) Enlist major features of TypeScript?
  • Question 8) What are Mixins in TypeScript?
  • Question 9) Why to use TypeScript?
  • Question 10) What are majpr Components of TypeScript?
  • Question 11) Enlist built-in data types available in TypeScript?
  • Question 12) What are TypeScript Decorators.
  • Question 13) What is JSX in TypeScript?
  • Sharad Jaiswal
  • 28th Dec, 2019
  • 676 Followers

Typescript Interview Questions
Updated ON

28th Dec, 2019

No. of Questions

19

Posted In

JavaScript

Author

Sharad Jaiswal

What is Typescript?

TypeScript is an object-oriented, strongly-typed compiled language and a set of tools. TypeScript is a superset of JavaScript with additional features. It adopts the basic building blocks of the program from Java Scripts and converts them into the latter for execution. TypeScript generated JavaScript reuses the entire JavaScript tools, frameworks, and libraries.

Any file that has .js can be converted into .ts compiled with other TypeScript files. It is portable in any type of device, browser, and OS and can execute in any environment that is compatible with JavaScript. Moreover, TypeScript does not need any particular runtime environment or dedicated VM to execute.

Q1. What is Typescript?

TypeScript is an open-source programming language. It is a superset of JavaScript that is developed and maintained by Microsoft. It is used to develop large applications and develop Javascript applications for both client-side and server-side.

TypeScript is an object-oriented, multi-paradigm, functional, and imperative programming language that was first released in 2012.

The compiler of the Typescript is written in Typescript and the programs are compiled to the JavaScript.

Q2. What are modules in TypeScript?

Modules are just organized code written in TypeScript. Modules are executed in their own scope. That is, the variables, functions, and the classes declared in the modules are not visible outside the scope of the module.

Modules are divided into two types.

  1. Internal Modules
  2. External Modules.

Q3. What are classes in TypeScript?

A class in Typescript is a blueprint for creating objects. As Typescript is an object-oriented programming language, it gives built-in support for creating the class. The class keyword is used to declare a class in the Typescript.

//syntax
class class_name { 
   //class scope 
}

The class definition can include fields, constructors, and functions. Fields are just variables. Constructors are used to allocating memory for objects. Functions are used to perform some actions in the fields.

Q4. What is static typing in Typescript?

Static typing is used in TypeScript in which the variable types are checked at the compile time. In static type, you must specify the type of each variable so common bugs can be caught at an early stage. Here, we are explicitly telling the compiler the type of each variable declared in the program.

Q5. What is use of tsconfig.json file in Typescript?

It is a configuration file that is used to specify the root level files and compiler options that are required to compile a TypeScript Object. This file in a directory specifies the directory as the TypeScript project root. The TypeScript configuration file can be extended from a different base configuration by using the extends property.

Q6. What are generics in TypeScript?

Generics are the reusable component in TypeScript that can work over a variety of types rather than a single one.

//example
function identity(arg: any): any {
    return arg;
}

Here, any keyword is generic that makes the function to accept any type of variable for the arg parameter. Type variable 'T' is also used to represent generics that work on types rather than values.

Q7. Enlist major features of TypeScript?

Some of the major features in the TypeScript are,

  • Object-oriented language - TypeScript supports Object Oriented programming with its features such as classes, interfaces, modules, inheritance, etc.
  • Portable - TypeScript is portable in the sense that it can run on any devices, operating systems, and browsers. It can run in any environment that JS is capable of running.
  • JavaScript is TypeScript and vice versa - The JavaScript file with .js extension can be easily converted into a TypeScript file by just changing the extension from .js to .ts. Also, the TypeScript code starts and ends with JavaScript. The code written in TypeScript is also compiled and converted into JavaScript for execution.
  • Support for JavaScript libraries - JavaScript libraries, frameworks, tools, can be used with the TypeScript.

Q8. What are Mixins in TypeScript?

A mixin class is used to implement some distinct aspects of functionality. It provides a form of code that can be accessed by other classes based on composing behavior.

Mixins create a partial class that is combined to form a single class that contains all the methods and properties from the partial class.

Q9. Why to use TypeScript?

The reasons to use TypeScript are,

  1. It is an open-source language that simplifies the JavaScript code. It also provides highly productive development tools for JavaScript IDE thus making the code easy to read and understand.
  2. It also avoids the common bugs that the developer runs into when developing with JavaScript by providing type checking. It also provides generics.
  3. It has support for modules, functions with REST parameters, and functions with optional parameters.
  4. Code written in TypeScript can be compiled as per ES5 and ES6 to support the latest browser. It also gives all the benefits of ES6 for more productivity.

Q10. What are majpr Components of TypeScript?

TypeScript can be divided into three main layers. They are the language, typescript compiler, and typescript language services.

Language - It is the TypeScript language element that contains the syntax, keywords, and type annotations.

Compiler - It is the program that converts the TypeScript code into JavaScript code for compilation. It performs functions such as parsing, and type checking.

Language services - These provide information that helps editors and other tools to give better features such as refactoring and Intellisense. It also supports some standard typical editor operations like code formatting, outlining, colorization, statement completion, and signature help.

Q11. Enlist built-in data types available in TypeScript?

The built-in data types available in TypeScript are number, string, boolean, enum, void, null, undefined, any, never, Array, and tuple.

Q12. What are TypeScript Decorators.

A decorator is a special kind of declaration that can be attached to a class, declaration, method, accessor, property or a parameter. It uses the expression form to evaluate a function. This function will be called at the run-time with the information about the decorated declaration.

The different decorators in the TypeScript are class decorator, method decorator, property decorator, and parameter decorator.

Q13. What is JSX in TypeScript?

JSX is JavsScript XML. It is a language that allows us to write HTML to React. TypeScript supports JSX embedding, type checking, and compiling directly to the JavaScript.

To use JSX, name the file with .tsx extension and enable JSX option. TypeScript by default comes with three JSX modes. They are preserved, react, and react-native.

The preserve mode is used to keep JSX as part of the output. The react mode doesn’t do through JSX transformation before use. The react-native mode is like preserve mode but its output will have a .js extension instead of .jsx like in the preserve.

Q14. Explian Type assertions in TypeScript?

TypeScript uses type assertion to notify the compiler about the type of the variable.

It is used at times when the type of the variable is unknown by the compiler. It is a compile-time feature.

//example
Let x: any = “hello”;
let s = (<string>x).substring(0,3);

Here, the angle bracket(<>) is used for Type assertions.

Q15. What is an Interface in TypeScript?

The interface is a structure that defines the syntax for the classes to follow. A class that implements an inheritance can inherit all the members of the interface. The interface is also used by the TypeScript compiler for type checking to check the structure of the object. An interface only contains the method declarations and the fields, but not the implementation of the functions. The ‘interface’ keyword is used to create an interface for the TypeScript.

//syntax
interface interface_name {  
          // variables' declaration  
          // methods' declaration  
} 

Q16. What are TypeScript Optional Properties?

Optional properties are written inside the interface to denote that these properties may be required or not based on some conditions. These optional properties are written similar to the other properties of the interface but it is denoted by the '?' at the end of the name.

//example
interface SquareConfig {
    color?: string;
    name?: string;
}

Q17. What is use of pipe symbol in TypeScript?

The pipe symbol is called a union type in the TypeScript.

It represents 'or'. That is, the values can be any one of the types listed with the pipe symbol.

//example
let x: (typeof Test1 | typeof Test2)[];

Here, the type of x can be test1, test2, or both in an array.

Q18. What is negative operator in TypeScript?

The negation operator is used to change the sign of a value.

//example
Var x = 1;
Var y = -x;
console.log(“value of y”, y);

Here, the value of y will be printed as -1;

Q19. What is duck typing in TypeScript?

Duck typing is a method to check the type of compatibility for complex variable types. It is used to compare one object with other objects by checking if the objects have the same matching type. Duck typing also checks that if the objects have the same property and variable types. If the property or variable type is not the same, then a compile-time error is generated. This checking provides safety in the TypeScript code.

About Author :

  • Author of Typescript Interview Questions

    Sharad Jaiswal

    Sharad Jaiswal is Sr. Web Developer from Noida area. He have rich experience in PHP, Angular Js, React, javascript and Node .If you any query or project on these programming you can drop your enquiry in comment section.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.