Updated March 3, 2023
Introduction to Typescript Interview Questions And Answers
Typescript is an open-source language that Microsoft developed. It acts as a superscript of JavaScript. It is mainly used when development is to be done for large applications. It can also be used when JavaScript applications are to be built on both client-side and server-side. It can be said as a language as well as a set of tools. It supports various JS libraries and is portable. Let us have a look at different questions that can be asked if you attend an interview on Typescript.
If you are looking for a job related to Typescript, you need to prepare for the 2023 Typescript Interview Questions. Every interview is different from the different job profiles, but still, to clear the interview, you need to have good and clear typescript knowledge. Here, we have prepared the important Typescript Interview Questions and answers that will help you succeed in your interview.
Below are the 10 important Typescript Interview Questions and Answers that are frequently asked in an interview. These questions are divided into parts are as follows:
Part 1 – Typescript Interview Questions (Basic)
This first part covers basic Interview Questions and Answers.
Q1) Explain what is Typescript, and how is it different from JavaScript?
Answer:
Typescript is a superscript of JavaScript and is used for the development of large applications. It provides optional static typing, classes, and interfaces. It can be said as a language and also a set of tools. It helps developers to use highly productive tools and helps in code refactoring. The main differences between Typescript and JavaScript are:
Typescript supports classes that help the programmer work more in an object-oriented way, while JavaScript uses reusable components with functions and prototype-based inheritance. JavaScript does not have any interfaces; on the other hand, typescript has interfaces. Static typing is supported in Typescript, while it is not supported in JavaScript. Typescript provides optional parameters; JavaScript does not.
Q2) Which are different data types that are supported by Typescript and explain how to implement inheritance?
Answer:
Typescript also supports data types provided by all other languages. It includes:
- Boolean: This can have values as true or false
- Number: This can be any number value
- String: This can be any character value
- Array: This can be a list of numbers together
- Enum: This allows for creating a user-defined data type.
Inheritance can be implemented in Typescript by using the extends keyword.
class Car {
public domestic:boolean;
constructor(public name: string) { }
}
class SUV extends Car {
constructor(name: string, domestic: boolean)
{
super(name);
this.domestic = true;
}
}
class Sedan extends Car {
constructor(name: string, domestic: boolean)
{
super(name);
this.domestic = false;
}
}
Let us move to the next Typescript Interview Questions.
Q3) Explain the tsconfig.json file?
Answer:
This file is used to indicate that the directory is the root of the Typescript project. This file specifies that root files and compiler options are required to compile that particular project. This file can also be used to streamline the building of the project. Below sample can be taken as an example:
{
"compilerOptions": {
"removeComments": true,
"sourceMap": true
},
"files": [
"main.ts",
"othermodule.ts"
]
}
Q4) Explain Lambda/Arrow functions in Typescript?
Answer:
The arrow function acts like an additional feature in typescript and is also known as the lambda function. This function is without a name.
var mulNum = (n1: number, n2: number) => n1 * n2;
In this example, => is a lambda operator and (n1 * n2) is the body of function, and n1,n2 are the parameters.
let addNum = (n1: number, n2: number): number => { return n1 + n2; }
let multiNum = (n1: number, n2: number): number => { return n1 * n2; }
let dividNum = (n1: number, n2: number): number => { return n1 / n2; }
addNum(10, 2);// Result - 12
multiNum(10, 2);// Result - 20
multiNum(10, 2);// Result – 5
Q5) What is the Anonymous function?
Answer:
This function is declared without any named identifier to refer to it.
var anonyFunc = function (num1: number, num2: number): number {
return num1 + num2;
}
//RESULT
console.log(anonyFunc(10, 20)); //Return is 30
//RESULT
console.log(anonyFunc(10, "xyz"));
// error: This will throw an error as string is passed instead of an integer.
Part 2 – Typescript Interview Questions (Advanced)
Let us now have a look at the advanced Typescript Interview Questions.
Q6) How can a class defined in a module be used outside the module?
Answer:
Classes defined in a module are available within the module and cannot be accessed outside the module.
module Vehicle {
class Car {
constructor (
public make: string,
public model: string) { }
}
var audiCar = new Car("Audi", "Q7");
}
var fordCar = Vehicle.Car("Ford", "Figo");
The variable fordCar will give an error as the class Car is not accessible and the user needs to use export keyword for the classes.
module Vehicle {
export class Car {
constructor (
public make: string,
public model: string) { }
}
var audiCar = new Car("Audi", "Q7");
}
var fordCar = Vehicle.Car("Ford", "Figo");
This variable will now work as export is used to make the Car accessible outside its module.
Q7) What are decorators, and list some of the decorators in TypeScript?
Answer:
Decorators enable a user to modify a class and its members. It allows the user to add annotations and Metaprogramming syntax for carrying out class declarations and members. These were just released on an experimental basis. Decorators can be enabled using a command line or by editing the tsconfig.json file. To enable decorators using command line following command should be used:
tsc --target ES5 --experimentalDecorators
Q8) How to compile a Typescript file?
Answer:
The following steps should be followed in order to compile a typescript file:
- A user must check if the Typescript engine is enabled or not. A user can go to the title bar and check for their username, and select options.
- In the project navigator, select and right-click the TS files that are to be compiled.
- Select compile to JavaScript
- A user can add a script reference to this compiled Javascript file in HTML code.
- Once this is done, the user can go to command line tsc <TypeScript File Name> to compile.
Let us move to the next Typescript Interview Questions.
Q9) What are the Interfaces in Typescript?
Answer:
The interface defines the syntax of any variable or entity. Interfaces define properties, methods, and various events. Here only members are declared. Interfaces help define various members and help in defining a structure for the deriving classes. Interfaces can be declared using the interface keyword.
Q10) Why is typescript called an optionally statically typed language?
Answer:
Typescript being optionally statically typed language means that compiler can ignore the type of variable. Using ‘any’ datatype user can assign any type of variable. Typescript will not throw any error.
var unknownType: any = 4;
unknownType = "Okay, I am a string";
unknownType = false; // A boolean.
Using this, any data type can be declared.
Recommended Articles
This has been a guide to the List Of Typescript Interview Questions and Answers. Here we have listed the best 10 interview sets of questions so that the jobseeker can crack the interview with ease. You may also look at the following articles to learn more –