Jun

Typescript Interview Questions
- Aniaya Murthy
- 07th Jun, 2022
- 597 Followers
Typescript Interview Questions
What is Typescript?
Typescript is a superset of JavaScript which primarily provides optional static typing, classes, and interfaces. We have written some Refined interview questions on Typescript that helps developers to crack interview on TypeScript.
Below are the list of some Typescript Interview Questions and their Answers that may be asked by an interviewer in your interview
Quick Questions about Typescript
Typescript is | subset of Javascript. |
Typescript is Licensed under | Apache License 2.0 |
Typescript is Designed by | Microsoft Corporation |
Typescript file extension | ts,.tsx |
Typescript is released | on 1 October 2012 |
Typescript is | based on Duck, gradual, structural typing discipline |
Typescript Interview Questions
1) What is Typescript?
Differences between Typescript and JavaScript are
- JavaScript don’t support Es6 while Typescript supports .
- JavaScript build up reusable components by using unctions and prototype-based inheritance while Typescript supports Classes that allow programmer to think in more object oriented way .
- JavaScript don’t have any interfaces while Typescript has interfaces.
- There is no static typing in JavaScript whereas there is static typing in Typescript.
- JavaScript has no optional parameter feature while Typescript has optional parameter feature.
2) What are generics in TypeScript?
Generics are a kind of tool which is consists of the ability to abstract types. It is used in TypeScript to create reusable code components that work with a varied range of types instead of a single type.
3) List some features of Typescript?
The Most popular Features of Typescript are:-
- Typescript can be compiled into all major versions of Javascript(ES3,ES5,ES6,ES7).
- Typescript can be used for cross-browser development and is an open-source project.
- Typescript is a superset of JavaScript that provides typed nature to your code.
- It is used to avoid the disadvantages of JavaScript like Typescript is designed to handle the needs of complex programs.
- The typescript debuted as a beta programming language on October 1, 2012, and since then has gone through many versions with improved capabilities.
- Another key feature of Typescript is in version Typescript 2.6 which covers error suppression comments.
- Typescript is more predictable and is generally easier to debug.
4) List some benefits of using Typescript?
- One of the biggest advantages of Typescript is its code completion and intelligence.
- 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 tries to extend JavaScript. Compiler generates JavaScript.
- Typescript is a backward compatible version of JavaScript that compiles to pure JavaScript which makes their writing easier and faster.
- Typescript is purely object oriented programming which means it is a programming paradigm based on the concepts of objects.
- Most important advantage is it offers a “compiler” that can convert to JavaScript equivalent code. And it has a concept of namespace defined by a “module”.
5) List the built-in types in Typescript?
These are also called the primitive types in Typescript. These are of 5 types: –
- Number type: it is used to represent number type values and represents double precision floating point values.
Syntax- var variable_name: number;
- String type: it represents a sequence of characters stored as Unicode UTF-16 code. It is the same as JavaScript primitive type.
Syntax- var variable_name: string;
- Boolean type: in Typescript, it is used to represent a logical value. When we use the Boolean type, we get output only in true or false. It is also the same as JavaScript primitive type.
Syntax- var variable_name: bool;
- Null type: it represents a null literal and it is not possible to directly reference the null type value itself.
Syntax- var variable_name:number=null;
- Undefined type: it is the type of undefined literal. This type of built-in type is the sub-type of all the types.
Syntax- var variable_name:number=undefined;
6) How to compile a Typescript file?
- It should be firstly verified that the Typescript engine has been enabled or not. In the title bar, click your username and select options.
- In the project navigator, select and right-click the TS files that are to be compiled.
- Choose compile to JavaScript.
- Add a <script> reference for the JS file in the HTML code if needed.
- To compile a typescript file via command line tsc <TypeScript File Name> command is used.
7) What are Modules in Typescript?
Modules are executed within their own scope, not in the global scope; this means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms.
Creating a Module
module module_name{ class xyz{ export foo(x, y){ return x*y; } } }
8) What are Mixins in TypeScript?
9) Who developed Typescript and what is the current stable version of Typescript?
10) Tell the minimum requirements for installing Typescript. Also, mention the steps involved in it.
Installing Typescript with the help of node and npm is recommended. Here, npm is used to install all the libraries and tools. The typescript should be installed globally using
npm install –g typescript
It installs a command line code “tsc” which will further be used to compile your Typescript code. Make sure that you check the version of Typescript installed on your system.
The following steps are involved in installing Typescript on your system:
- Download and run the .msi installer for 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
11) What are variables in Typescript? How to create a variable in Typescript?
- A variable name should contain alphabets and numeric digits.
- It cannot contain spaces and special characters except underscore (_) and dollar ($) sign.
- A variable name cannot begin with a digit.
You should always keep in mind, that a variable should be declared before being used. Variables in Typescript are declared by placing a “var” keyword prior to the variable name. A variable can be declared using 4 methods: –
- Declare its type and value in one statement.Syntax- var variable_name:string = value;
- Declare its type but no value.Syntax- var variable_name:string;
- Declare its value but no type.Syntax- var variable_name = value;
- Declare neither value nor type.Syntax- var variable_name;
12) What do you mean by interfaces? Explain them with reference to Typescript.
Syntax-
interface interface_name{ Statements; }
Interfaces need not to be converted to JavaScript for execution. They have zero runtime JavaScript impact. Interfaces can define both the kind of keys which an array uses and the type of entry it contains.
13) What do you understand by classes in Typescript? List some features of classes.
Example
class Greeter { greeting: string; constructor (message: string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; } }
Some features of a class are-
- Encapsulation i.e. creation of self-contained modules that bind processing functions to the data, takes place.
- Classes are created in hierarchies and with the help of inheritance; the features of one class can be used in the other.
- Polymorphism takes place.
- Abstraction
14) Is Native Javascript supports modules?
Looking For Angular 2 Interview Questions. Click here
15) How to compile multiple Typescript files into a single file?
tsc --outFile outputfile.ts file1.ts file2.ts file3.ts ... filen.ts
16) How to Call Base Class Constructor from Child Class in TypeScript?
17) What are Closures in Javascript?
Closures are nothing but a statefull functions.
A closure is an inner function that has access to outer function’s variables in addition to it’s own variable and global variables.In simple term a closure is a function inside a function.Closures using Lexical scoping in which function have access to global and outer variables.Below is a sample example of Javascript closure.
function employee() { var employee_dept = 'IT'; return { getDept: function () { return employee_dept; }, setDept: function (new_dept) { employee_dept = new_name; } } } var emp1 = employee (); // In this juncture, the employee_dept outer function has returned. mjID.getDept(); // IT mjID.setDept('Account'); // Changes the outer function's variable mjID.getDept(); //outputs Account
Closures prevents your important variable to be modified accidently.It can be only modified by itself.
18) List types of scopes available in Javascript?
- Global Scope
- Local Scope
19) What is namespace in Typescript? How to declare a namespace in Typescript?
Synatax for creating namespace in Typescript
namespace YourNameSpace { export class Class1 { } export class Class2 { } }
20) Explain Decorators in Typescript? List type of Decorators available in Typescript?
Decorators are function that supports annotating or modifying classes its members.Its allow way to add both annotations and a meta-programming syntax for class declarations and members. Decorators are an experimental feature of Typescript and maybe change in future releases.
You can enable Decorators using the command line or editing your tsconfig.json
Enabling Decorators in TypeScript via command line
tsc --target ES5 --experimentalDecorators
21) What is default visibility for properties/methods in Typescript classes?
22) Explian Type assertions in TypeScript?
Type assertion allows you to set the type of a value. After the value set, typescript told the compiler not to assume the type of value.
23) What is an Interface in TypeScript?
The classes in TypeScript must follow the structure of the interface. The interface contains the method declaration, variable declaration.
24) What are TypeScript Optional Properties?
The optional properties use as an optional thing. The interface typescript has many properties but every property not required. You can pass the object into interface using ( ? ) question mark symbol.
25) What is an implicit Module in Typescript?
A module is used to create a set of related variables, functions, classes, and interfaces, etc in the Typescript. The internal and external module has two categories of Typescript.
26) What is duck typing in TypeScript?
Duck-typing used to check the type compatibility for more complex variable types in typescript. This method checks the type matching between two objects. If the property contains more or less or not match then a compile-time error occurred.
Leave A Comment :
Valid name is required.
Valid name is required.
Valid email id is required.