TYPESCRIPT INTERVIEW QUESTIONS AND ANSWERS 2020

[Total: 1    Average: 5/5]

Q1 What Is TypeScript? 

By definition, “TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.” 

TypeScript is a superset of JavaScript which provides optional static typing, classes and interfaces. 

 => The TypeScript was first made public in the year 2012. 

=> Typescript is a modern age JavaScript development language. 

=> TypeScript is a strongly typed, object oriented, compiled language. 

=> TypeScript was designed by Anders Hejlsberg (designer of C#) at Microsoft. 

=> TypeScript is both a language and a set of tools. 

 

Q2 What Are the Features of TypeScript? 

1) Maintainability 

2) Offered great productivity for developers 

3) Code navigation and bug prevention 

4) Code ‘discoverability’ & refactoring 

5) Optional Static Type Annotation 

6) Additional Features for Functions 

7) Supports ES6 

8) TypeScript supports other JS libraries. 

9) Supports interfaces, sub-interfaces, classes, and subclasses 

10) Scalable HTML5 client-side development 

11) Rich IDE available with autocomplete and code navigation features. 

12) Class-based object-oriented with the inheritance of private members and interfaces 

13) Typescript is a powerful type system, including generics & JS features 

14) Static Typing 

 

Q3 What Are the Benefits of Using TypeScript? 

1.     Supports Object Oriented Programming. 

2.     Typescript adds static typing to JavaScript. Having static typing makes easier to develop and maintain complex apps. 

3.     Angular uses TypeScript a lot to simplify relations between various components and how the framework is built in general. 

4.     Provide an optional type system for JavaScript. 

5.     Provide planned features from future JavaScript editions to current JavaScript engines. 

6.     Supports type definitions. 

7.    Increased team performance. 

8.   Rich IDE support with autocomplete and code navigation features. 

9.   Module support 

 

List of some other Advantages, 

  •   It is purely object-oriented programming. 
  • It is support static type-checking. 
  • It can be used for client-side and server-side development. 
  • Build-in Support for JavaScript Packaging 
  • It offers a “compiler” that can convert to JavaScript-equivalent code. 
  • It has an API for DOM manipulation. 
  • It has a namespace concept by defining a “Module”. 
  • Superset of JavaScript 
  • ES6 features support 

 

Q4 What Are the Disadvantages of TypeScript? 

=> Overly complicated typing system 

=> Required compilation 

=> False sense of security 

=> Compilation can be time-consuming 

=> If you are developing very large enterprise software, every compilation step increases the time to build 

 

Q5 What Is other strictly typed languages that compile to JavaScript? 

=> Kotlin has an official JavaScript compilation solution. 

=> Rust may work on the frontend; however, it doesn’t compile directly to JavaScript, but rather WebAssembly. This WebAssembly code can then be combined and used together with JavaScript code. 

=> Go can be effectively compiled to JavaScript—via an unofficial tool called GopherJS—or to WebAssembly, much like Rust. 

=> Elm and Reason were designed for JavaScript compilation. 

=> Bridge.NET (C#) 

=> Cheerp (C++) 

=> Dart 

=> GopherJS (Go) 

=> Haxe 

=> JSweet (Java) 

=> Scala.js (Scala) 

 

Q6 What Are the Filename extensions of TypeScript? 

The TypeScript filename extensions are, 

 => .ts 

 => .tsx 

 

Q7 What Are difference between JavaScript and TypeScript? 

TypeScript design and developed by Anders Hejlsberg at Microsoft. 

JavaScript – Brendan Eich at Netscape communications corporation, Mozilla Foundation, ECMA International. 

 

=> TypeScript has a feature known as Static typing but JavaScript does not have this feature. 

=> The file extension of TypeScript are (.ts/.tsx) while the file extension of JavaScript is (.js) 

=> TypeScript has Interface but JavaScript does not have Interface. 

=> TypeScript specially used in client-side but JavaScript use both client-side and server-side. 

=> TypeScript support optional parameters function but JavaScript does not support the optional parameters function. 

=> TypeScript is a heavy weighted an interpreted programming language but JavaScript is a light weighted. 

 

Q8 What Is TypeScript Types? 

The Type represents the different types of values which are using in the programming languages and it checks the validity of the supplied values before they are manipulated by your programs. 

The TypeScript provides data types as a part of its optional type and its provide us some primitive types as well as a dynamic type “any” and this “any” work like “dynamic”. 

 

Q9 What Is scope variable? 

The scope is set of objects, variables and function and the JavaScript can have global scope variable and local scope variable. 

We can say, we can declare a variable in the two type’s i.e. 

1.     Local Scope Variable 

2.     Global Scope Variable 

       Global Scope Variable - The global scope is a window object and it’s used out of function and within the functions. 

Local Scope Variable - The local scope is a function object and it’s used within the functions. 

 

Q10 What Is an Interface in TypeScript? 

An interface in TypeScript is similar to other object oriented programming languages interfaces. 

An interface is a way to define a contract on a function with respect to the arguments. 

In the below example, I am using an interface that describes objects that have a “name”, “age” and “address” fields and the following code defines an interface and a function that takes a parameter that adheres to that interface. 

 

Q11 What is Optional Properties in TypeScript? 

We can specify optional properties on interfaces and the property may be present or missing in the object. 

 

Q12 What Is the Functions in TypeScript? 

How many types you defined in TypeScript? 

A function is a set of statements that perform a specific task and used to create readable, maintainable and re-usable code. 

A function declaration tells to compiler about a function name, return type, and parameters. 

TypeScript functions are almost similar to JavaScript functions but there are different ways of writing functions in TypeScript. 

The Different Types of Available Functions in TypeScriptare 

1.     normal function 

2.     Anonymous function 

3.     Named function 

4.      lambda function 

5.     Class Function 

6.     optional function 

7.     rest parameters 

8.     default parameters 

 Q13 How to create fields, constructor and function in TypeScript Class? 

What is class in TypeScript? 

A class is a template definition of the methods and variables in a particular kind of object. It is extensible program, code and template for creating objects. 

TypeScript is object oriented JavaScript and it also supports object oriented programming features like classes, interfaces, etc.
A class captains the Public, Private, Protected and Read-only modifiers and Public by default.
You can see the below example, the class User and each members are public by default. 

A class definition can contains the following – 

1.     Fields 

2.     Constructors 

3.     Functions 

 

Q14 What Is Inheritance in TypeScript? 

The TypeScript supports the concept of Inheritance. 

Inheritance has ability of a program to extend existing classes to create new ones and the extended class is called parent class or super class and the newly created classes are called child class or sub class.
Inheritance can be classified as – 

1.       Single – every class can at the most extend from one parent class 

2.     Multiple – doesn’t support multiple inheritances in TypeScript. 

3.     Multi-level 

 

Q15 What Are the Parameter Property in TypeScript?
The TypeScript has so many useful features which not have in other programming languages. The TypeScript has an automatic assignment of constructor parameters that is called “Parameter Property”. 

It is automatic assignment of constructor parameters to the relevant property. It is great but it is different to other languages. 

 

Q16 Which access modifiers are implied when not specified? 

Everything in a class is public if not specified. Everything in a module is private unless export keyword is used. 

 

Q17 What Is the purpose of the public access modifier for classes in Typescript? 

Your sample code means exactly the same in TypeScript. When you don’t put a modifier public, private or protected on your member definition then TypeScript will choose the default one which is public. 

 

Q18 What Is a Module in TypeScript?  

The module system is an interesting feature of TypeScript, the statically typed superset of JavaScript. Modules provide the possibility to group related logic, encapsulate it, structure your code and prevent pollution of the global namespace. 

 

 

Q19 What Is an Internal Module in TypeScript?  

You can define modules within your typescript files and all variables defined within the module are scoped to the module and removed from the global scope. 

You can access the variable outside the module using the export keyword and also you can extend internal modules, share them across files, and reference them using the triple slash.  

Q20 What Are The Advantages of Module? 

The list of Advantages Are– 

1.     Code reuse 

2.     Encapsulation 

3.     Scoping of variables 

4.     Support CommonJs 

5.     Easier for testing 

 

Q21 What Is Implicit Internal Modules? 

You simply write source code without worrying about modules that is called implicit internal modules. 

 

Q22 What Is NamedInternal Modules? 

Internal modules are come in earlier version of the Typescript and it was basically used to logically group classes, interfaces, and functions into one unit and it can be export in another module. 

 

Q23 What Is difference between module and namespace in typescript? 

Module is for external packages and the namespace is for internal packages. Actually, the module keyword has been replaced with the namespace keyword. 

Namespaces are simply named JavaScript objects in the global namespace. 

Modules can contain both code and declarations. The main difference is that modules declare their dependencies. 

The named modules called “namespace” in latest version of TypeScript. So we can use namespace instead of internal modules in the TypeScript. 

As per me, this one is the best coding practice but don’t mind the internal modules are also supporting, if you want can use it. 

 

Q24 Is it possible to combine multiple .ts files into a single .js file? 

yes, It is possible. 

 

Q25 What is the syntax to declare ambient variables in TypeScript? 

The syntax to declare ambient variables: 

declare module Module_Name { 

declare module Module_Name { 

} 

 

Q26 What is Internal Module in TypeScript? 

Internal Module: It is used to logically group classes, interfaces, functions into one unit and can be exported in another module. 

 

Q27 What is the syntax to declare External Module using export in TypeScript? 

The syntax to declare External Module using export is: 

 

//FileName:TutorialandExample.ts 

export interfaceTutorialandExample { 

   //code declarations  

} 

 

Q28 What is the syntax to declare External Module using import in TypeScript? 

The syntax to declare External Module using import is: 

  1. importTutorialandExampleRef = require(“./TutorialandExample); 

 

Q29 What is the syntax to declare Internal Module in TypeScript? 

module TutorialandExample { 

   export function add(x, y) { 

      console.log(x+y); 

   } 

} 

 

Q30 What is Nested Namespaces in TypeScript? 

Nested Namespaces: we can define one namespace inside another namespace. 

 

Q31 What is the syntax to declare Nested Namespace in TypeScript? 

The syntax to declare Nested Namespace is: 

namespace firstnamespace { 

   export namespace secondnamespace { 

      export classclassname {    } 

   } 

} 

 

Q32 What is Object in TypeScript? 

Object: It is an instance of class which contains set of key and value pairs. 

Here, the values can be scalar values/functions or array of other objects. 

 

Q33 What is Duck-typing in TypeScript? 

Duck-typing: It is used to verify the presence of certain properties in the objects to check their suitability. 

Here, two objects are considered to be of the same type if both share the same set of properties. 

 

Q34 What is the use of this keyword in TypeScript? 

this keyword: It refers to the current instance of the class. In this keyword, the parameter name and the name of the class field are the same. 

 

Q35 What is the use of new keyword? 

new keyword: It is used for instantiation in object creation. 

 

Q36 What are the types of inheritance classified in TypeScript? 

The types of inheritance classified in TypeScript are: 

  • Single 
  • Multiple 
  • Multilevel 

 

Q37 What are the types of access modifiers supported by TypeScript? 

The types of access modifiers supported by TypeScript are: 

  • Public 
  • Private 
  • Protected 

 

Q38 What is the use of pipe symbol in TypeScript? 

pipe symbol: It is used to combine two or more data types to denote a Union Type. 

 

Q39 What is the use of push() and pop() method in Tuple? 

push() method: It appends an item to the tuple. 

pop() method: This method removes and returns the last value in the tuple. 

 

Q40 What are the variable scopes available in TypeScript? 

The variable scopes available in TypeScript are: 

  • Global Scope 
  • Class Scope 
  • Local Scope 

  

Q41 What is the role of var keyword in typescript. 

var is key it helps us to creating an object of a class. 

 

Q42 How to name interface? 

According to official coding guidelines, you should use PascalCase for types names. Also don’t use “I” prefix in interface names. For interface implementation Impl suffix can be added(something like WritableImpl) 

 

Q43 What is union type ? 

Variable should be declared with union type if it can be set with values of different types. It can be useful when you need to return correct result or null value, for example in find function. 

 

Q44 What is duck typing ? 

Duck typing consider two object have the same type, if they have same set of properties. Declaring object with literal is equal to creating object with new operator 

 

Q45 How to pass options to TypeScript compiler? 

Through command line arguments or tsconfig.json file. If file tsconfig.json located in some directory, that means this root of TypeScript project. There are such options available: compilerOptions,files,typeRoots, include, exclude. 

 

Q46 There is some way to automatically recompile project if some file changed? 

Yes, run tsc command with -w argument, also pass –no-initial-compile option if you want only watch changes. 

 

Q47 How to use JSX in TS project? 

To use JSX you need, save your code in files with *.jsx extension and enable jsx option in tsconfig.json. There is three JSX modes react,preserve, react-native. 

 

Q48 What is generics ? How to use them? 

Generic is a some generalized type, which can substituted by any concrete type like string or number. Generics allow to write universal function, with type safety, without using “any” type. There is generic functions,classes, or interfaces, for example you can write Container class, which implement basic collection operations, regardless type of stored elements. 

 

Q49 What are types in TS? 

Types in Typescript define the data-type. It helps in avoiding the wrong value passing. There are 2 different types: 

  • Built in — String, Number, boolean, Null, Undefined, any 
  • User Defined — Array, object, classes, interfaces 

 

Q50  How you can use TS with jQuery, Angularjs and Reactjs? 

Angularjs is already integrated with Typescript.So, if you are using Angularjs CLI you don’t need to do anything special. For React and jQuery use Typescript’s @types and it will install all the dependencies. @types/ React @types / jQuery. 

 

 

writer
Leave a Reply

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages