In previous article, I have mentioned AWS Lambda Interview Questions and Answers and Javascript Multiple Choice questions and answers but in this article, I have mentioned useful beginner and advanced Typescript Interview questions with Answers.

typescript-interview-questions-and-answers.png

What is TypeScript and why do we need it?

TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide a richer environment for spotting common errors as you type the code.

For a large JavaScript project, adopting TypeScript might result in more robust software, while still being deployable where a regular JavaScript application would run.

TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language. TypeScript is quite easy to learn and use for developers familiar with C#, Java and all strong typed languages.

TypeScript adds additional syntax to JavaScript to support a tighter integration with your editor. Catch errors early in your editor.

What are the benefits of using Typescript?

TypeScript has following benefits.

  • It helps in code structuring.
  • Use class based object oriented programming.
  • Impose coding guidelines.
  • Offers type checking.
  • Compile time error checking.
  • Better Intellisense.

How can you get TypeScript and install it?

TypeScript can be installed and managed via npm, the Node.js package manager.

To install TypeScript, you must have Node.js installed and npm installed. And then run following command to install TypeScript globally on your system.

npm install -g typescript

How do you compile TypeScript files?

Suppose, we have created a file with name "example.ts", then to compile it , we can use below command

tsc example.ts

What are the different data-types supported by TypeScript?

Here are the data-type supported by typescript:

  1. boolean: Represents true and false
  2. string: It is used to represent a sequence of characters
  3. void: Generally used on function return-types
  4. null: It is used when an object does not have any value
  5. enum: Enumerated data types (enums) are a set of numeric values with more friendly names.
  6. Array: Just like JavaScript, you can work with arrays in TypeScript and can define them
  7. undefined: Denotes value given to uninitialized variable
  8. any: If variable is declared with any data-type then any type of value can be assigned to that variable

What is AMD in TypeScript?

In Typescript, the Asynchronous Module Definition (AMD) is used to specify a process for defining modules in a manner such that modules and their dependencies can be loaded asynchronously

This is greatly helpful in a browser environment where performance, usability, debugging, and access to cross-domain problems occur due to the synchronous loading of modules.

What is tsconfig.json file?

The tsconfig.json file specifies the root files and the compiler options required to compile the project. The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project

The file tsconfig.json is a JSON-based format.

Example:

{
   "compilerOptions": {
      "declaration": true,
      "emitDecoratorMetadata": false,
      "experimentalDecorators": false,
      "module": "none",
      "moduleResolution": "node"
      "removeComments": true,
      "sourceMap": true  
   },  
   "files": [
      "main.ts",  
      "othermodule.ts"  
    ]  
}  

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

Yes, we can provide -output command and combine files, like below

tsc --outFile comman.js example1.ts example2.ts example3.ts

This will compile all 3 ".ts" file and output into single "comman.js" file

How check if a type is a union in Typescript?

Here is the sample code for it

type IsUnion<T, U extends T = T> = 
  T extends unknown ? [U] extends [T] ? false : true : false;

type Test = IsUnion<1 | 2> // true
type Test2 = IsUnion<1> // false
type Test3 = IsUnion<never> // false

The trick here is distributing T but not U so that you can compare them. So for type X = 1 | 2, you end up checking if [1 | 2] extends [1] which is false, so this type is true overall. If T = never we also resolve to false

In Typescript, what is the ! (exclamation mark) operator when dereferencing a member?

That's the non-null assertion operator. It is a way to tell the compiler "this expression cannot be null or undefined here, so don't complain about the possibility of it being null or undefined."

Can you erase the values of array elements in TypeScript?

No, the values of array elements can only be modified but not erased in TypeScript.

What is the function of the Omit type in TypeScript?

The omit utility type was introduced in TypeScript release 3.5 and it helps developers to generate new type definitions by omitting or excluding properties of an existing group to construct a new group, which is a subgroup of properties of an existing group.

What does the Record type in TypeScript do?

TypeScript Records are a great way to ensure consistency when trying to implement more complex types of data. They enforce key values, and allow you to create custom interfaces for the values.

Example

type User = {
    firstName: string,
    lastName: string
}

const myData:Record<string, User> = {
    "123-123-123" : { firstName: "John", lastName: "Doe" },
    "124-124-124" : { firstName: "Sarah", lastName: "Doe" },
    "125-125-125" : { firstName: "Jane", lastName: "Smith" }
}

A Record takes the form Record<K, T>, where K is the type of the key, and T is the type of the values.

Above, we defined a new type User for our values, and set our keys to type string.

What is the function of the .map file in TypeScript?

.map files are source map files that let tools map between the emitted JavaScript code and the TypeScript source files that created it.

Many debuggers (e.g. Visual Studio or Chrome's dev tools) can consume these files so you can debug the TypeScript file instead of the JavaScript file.

The .map can be used by setting the sourcemap compiler option as true in the tsconfig.json.

You may also like to read:

Top C# Interview Questions and Answers

ASP.NET Core Interview questions and answers

C programming Interview Questions with Answers