Complete Guide to Typescript Interview Questions (2020) | TechGeekNext >>





The Ultimate Guide to Typescript Interview Questions and Answers (2020)


TypeScript is a JavaScript super-set developed by Microsoft, which is strongly typed.

TypeScript has the best "JavaScript(ES6)" and "Modern Language features (Annotation, Inerfaces, Gernerics)" together which provides similar syntex and functionality like other middle layer languages (like Java, C #). These make it easy for Full Stack Developers to implement. TypeScript is gaining popularity very quickly, and the user community is getting bigger every day.

Google has recently announced that it will start using Typescript internally alongside Java, so Full stack development in Typescript using Node as a runtime will likely become mainstream in the next few years.

In this post, questions from Typescript Interviews will be answered for Experienced and Freshers. We're trying to share our experience and learn how to help you make progress in your career.

  1. What is TypeScript? What are the Benefits of using TypeScript ?
  2. What are the object-oriented terms supported by TypeScript ?
  3. TypeScript vs JavaScript ?
  4. What are the different built-in data types supported by TypeScript ?
  5. What are Interfaces in TypeScript ?
  6. What are Classes in TypeScript? List out some of the features. ?
  7. What is tsconfig.json file ?
  8. What are Generics in TypeScript ?
  9. What are TypeScript collections ?
  10. Explain Enum in TypeScript.
  11. What is Lambda/Arrow function ?
  12. Does TypeScript supports function overloading ?

Q: Why is TypeScript?
Ans:

TypeScript is a superset of JavaScript. Its emphasis on strong typing and advanced language constructs help us write client and server-side code with fewer errors.

TypeScript is cross-platform, open source, and compiles to JavaScript that is compatible with every major browser and popular JavaScript framework. Some of the major concepts are functions, interfaces, classes, modules and namespaces, and generics

The JavaScript language has a handful of types like string, number, and Boolean, but TypeScript lets you define your own types in a very object-oriented manner, and the compiler will then check your code against those types and alert you to problems long before it gets to a production environment. I happen to believe that the addition of types also enables faster development.

If you're using a TypeScript-aware editor (VSCode), it becomes a lot easier to examine the properties and methods available in custom types, and navigate around your project based on the symbols declared in your code.

Q: TypeScript vs JavaScript?
Ans:

TypeScript JavaScript

TypeScript is an Object-Oriented language

JavaScript is a Scripting language

It has a feature known as Static typing

It does not have static typing

TypeScript gives support for modules

JavaScript does not support modules

It supports optional parameter function

It does not support optional parameter function

Q: What are the different types supported by TypeScript ?
Ans:

String: A string is a series of characters that are stored as UTF-16 Unicode code
Number: It reflects the values of the number form. The numbers are stored as floating-point values in TypeScript
Boolean: This can have values as true or false
Array: This can be a list of numbers together
Enum: This allows creating a user-defined data type.

Q: What are TypeScript collections ?
Ans:

Like other programing languages Typescript also has support for Collection, like Map, Set etc.

To know more about Typescript Collections, click here

Q: What are Generics in TypeScript ?
Ans:

Generics are a feature of TypeScript, which allows you to take advantage of that type safety while creating code you can reuse throughout your apps.
  • Reusable type-safe code that works with multiple types
  • May be functions, interfaces, or classes
  • Code that accepts type parameters for each instance or invocation
interface DataStructure<T> {
  push(newItem: T): void;
  pop(): T;
}

class Stack<T> implements DataStructure<T> {
  
  items: Array<T> = [];
  
  push(newItem: T): void {
    this.items.push(newItem);
  }
  
  pop(): T {
    return this.items.pop();
  }

  peek(): T {
    return this.items[this.items.length - 1];
  }
}

let numberStack = new Stack<number>();

numberStack.push(10);
numberStack.push(20);
numberStack.push(30);

console.log(numberStack.pop()); // 30
console.log(numberStack.peek()); // 20
console.log(numberStack.pop()); // 20
console.log(numberStack.pop()); // 10

Q: Explain Enum in TypeScript. ?
Ans:

Enums  are a data form TypeScipt that enables us to define a set of named constants. Enums may facilitate recording purposes or setting up a number of different cases. It is a collection of related values, numeric or string values.
enum Size {  
  Big,  
  Medium  
  Small  
}  
console.log(Size.Medium); // Output: 1  
//We can also access an enum value by it's number value.  
console.log(Size[1]); // Output: Medium  

Q: What is tsconfig.json file ?
Ans:

The file tsconfig.json is a JSON-based format. We may define multiple options to display the compiler how to create the current project in the tsconfig.json file. The presence of the file tsconfig.json in a directory indicates that the folder is the root of the project TypeScript. The sample tsconfig.json is described below.
{  
   "compilerOptions": {  
      "declaration": true,      
      "emitDecoratorMetadata": false,      
      "experimentalDecorators": false,      
      "module": "none",      
      "moduleResolution": "node"  
      "removeComments": true,  
      "sourceMap": true  
   },  
   "files": [  
      "main.ts",  
      "othermodule.ts"  
    ]  
}  

Q: What is Lambda/Arrow function ?
Ans:

ES6 TypeScript version provides a shorthand syntax, i.e. for function expressions, to define the anonymous function. The functions of this arrow are known as Lambda functions. A lambda function is a function without a name. The function arrow omits the keyword function.
let sum = (a: number, b: number): number => {    
            return a + b;    
}    
console.log(sum(10, 20)); //returns 30    
We = > operator means arrow / Lambda operator in the example as mentioned previously. The parameters a and b here are, and the function body (a + b) is considered.

Q: Does TypeScript supports function overloading ?
Ans:

Yes, TypeScript support function overloading. However, the execution is strange. Only one function with multiple signatures can be implemented when we perform overload in TypeScript.
//Function with string type parameter    
function add(a:string, b:string): string;    
  
//Function with number type parameter    
function add(a:number, b:number): number;    
  
//Function Definition    
function add(a: any, b:any): any {    
    return a + b;    
}    
The first two lines are the overload declaration function in this example. It is overloaded by two. The former has a string parameter while the former has a type number parameter. The third function contains the actual execution and has a type parameter. Any type of data can take data of any kind. The execution then checks the parameter type and executes another code based on the supplier parameter type.

Q: What are the interfaces in Typescript ?
Ans:

The interface defines the syntax for any variable or entity. Interfaces define properties, methods, and events. Only the members are declared here. Interfaces are useful in defining different members and help to define the structure of the drifting classes. Interfaces can be declared using the keyword of the interface.
interface Employee { 
   name: string,
   dob: date,
   address: string,
   getDepartment: ()=>string 
}


































































Recommendation for Top Popular Post :