TypeScript interview questions and answers

TypeScript is a superset of the JavaScript language and it is mainly developed and maintained by Microsoft. TypeScript is immensely popular in writing front-end applications. It is also used in Node.js too.

Following are some useful interview questions about TypeScript:

How does optional chaining work in TypeScript?

The optional chaining in TypeScript reduces the chances of causing errors. It immediately stops executing the following expressions if it gets null or undefined.

The operator is used for optional chaining is: ?.. Following is an example:

let x = user?.role.getRole();

Provide the TypeScript syntax to create function overloads?

TypeScript supports function overloading based on a number of parameters. The function overloading is quite different compare to other object-oriented languages.

Following is an example of TypeScript function overloading:

  class User {
    getAttr(attr: string);
    getAttr(attr: number);
    getAttr(attr: number, secondAttr: string);
    getAttr(attr: any, secondAttr?: string) {
      console.log(attr.toString());
    }
  }
What is type inference?
What is meant by contextual typing?
What is an interface?
How to control member visibility in TypeScript?
What are abstract classes in TypeScript?
What are anonymous functions in TypeScript?
What are union types in TypeScript?
What are intersection types?
What are type aliases?
What are the tuple types in TypeScript? How does tuple destructuring works in TypeScript?
What are type assertions in TypeScript?
How to enforce strict null checks in TypeScript?
How to make object properties immutable in TypeScript?
What is a type declaration file?
What are triple-slash directives?
What are the ‘implements’ clauses in TypeScript?
What are string literal types?
What are template literal types?
Explain the concept of inheritance in TypeScript.
What is the Function type in TypeScript?
What are the variants of for loop in TypeScript?