Typescript interview questions and answers 👇

  1. TypeScript General Interview Questions

TypeScript General Interview Questions

Difference between typescript and javascript?

View answer

  • Typescript is a superset of JavaScript whereas JavaScript is a scripting language.
  • Typescript has an interface concept but JavaScript havan't.
  • TypeScript doesn't run in the browser where JavaScript runs.
  • TypeScript has classes whereas JavaScript has functions.
  • Typescript support module whereas JavaScript doesn't support modules.
  • In typescript Number, a string is the interfaces whereas In, JavaScript Number, string are the Object.

How do you install TypeScript?

View answer

npm install -g typescript

Who developed Typescript and what is the current stable version available?

View answer

Anders Hejlsberg developed TypeScript. The current stable version of TypeScript is 3.9.7.

What are types in TS?

View answer

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

What Are The Benefits Of Typescript?

View answer

TypeScript has the following benefits:

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

What are Variables in TypeScript?

View answer

A variable is a named space in the memory which is used to store values. The type syntax for declaring a variable in TypeScript includes a colon (:) after the variable name, followed by its type.

What are Interfaces in TypeScript?

View answer

The interface is a structure that defines the contract in your application. It defines the syntax for classes to follow. It contains only the declaration of the members and it is the responsibility of the deriving class to define the members. The TypeScript compiler uses interface for type-checking and checks whether the object has a specific structure or not.

What are Classes in TypeScript?

View answer

TypeScript introduced classes so that they can avail the benefits of object-oriented techniques like encapsulation and abstraction. The class in TypeScript is compiled to plain JavaScript functions by the TypeScript compiler to work across platforms and browsers.

What are the access modifiers supported by TypeScript?

View answer

  • Public – All the members of the class, its child classes, and the instance of the class can access.

  • Protected – All the members of the class and its child classes can access them. But the instance of the class can not access.

  • Private – Only the members of the class can access them.

What are modules in TypeScript?

View answer

A module is a powerful way of creating a group of related variables, functions, classes, and interfaces, etc. It can be executed within its own scope, but not in the global scope. Basically, you cannot access the variables, functions, classes, and interfaces declared in a module outside the module directly.

What is namespace in Typescript?

View answer

Namespace groups functionalities logically. These maintain the legacy code of typescript internally. It encapsulates the features and objects that share certain relationships. A namespace is also known as internal modules. A namespace can also include interfaces, classes, functions, and variables to support a group of related functionalities.

Explain Decorators in TypeScript.

View answer

A Decorator is a special kind of declaration that can be applied to classes, methods, accessor, property, or parameter. Decorators are functions that are prefixed @expression symbol, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.

What are Mixins?

View answer

In Javascript, Mixins are a way of building up classes from reusable components and then build them by combining simpler partial classes.

The idea is simple, instead of a class A extending class B to get its functionality, function B takes class A and returns a new class with this added functionality. Here, function B is a mixin.

What is Scope variable?

View answer

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

You can declare a variable in two different scopes such as:

Local Scope Variable – It is a function object which is used within the functions

Global Scope Variable – You can use this window object out of function and within the functions

What is the TypeScript Definition Manager?

View answer

TypeScript Definition Manager (TSD) is a package manager used to search and install TypeScript definition files directly from the community-driven DefinitelyTyped repository.

What is tsconfig.json file?

View answer

The tsconfig.json file is a file which is in JSON format. In the tsconfig.json file, you can specify different options to tell the compiler how to compile the current project. The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project.

What are Generics in TypeScript?

View answer

TypeScript Generics is a tool that provides a way of creating reusable components. It is able to create components that can work with a variety of data types rather than a single data type. Also, it provides type safety without compromising the performance, or productivity. Generics allow us to create generic classes, generic functions, generic methods, and generic interfaces.

What is JSX in TypeScript?

View answer

JSX is an embeddable XML-like syntax and it is meant to be transformed into a valid JavaScript. JSX became popular with the React framework. TypeScript supports embedding, type checking, and compiling JSX directly into JavaScript.

What is a TypeScript Map file?

View answer

TypeScript Map file is a source map file that holds information about our original files. The .map files are source map files that let tools map between the emitted JavaScript code and the TypeScript source files that created it. Also, debuggers can consume these files so we can debug the TypeScript file instead of the JavaScript file.

What is “as” syntax in TypeScript?

View answer

The “as” is the additional syntax for Type assertion in TypeScript. The reason for introducing the as-syntax is that the original syntax conflicted with JSX.

Explain Enum in TypeScript.

View answer

Enums or enumerations are a TypeScipt data type that allows us to define a set of named constants. Using enums make it easier to document intent, or create a set of distinct cases. It is a collection of related values that can be numeric or string values.

What is an anonymous function?

View answer

An anonymous function is a function that is declared without any named identifier. These functions are dynamically declared at runtime. Also, anonymous functions can accept inputs and return outputs, just as standard functions do. It is usually not accessible after its initial creation.

What is Lambda/Arrow function?

View answer

ES6 version of TypeScript provides shorthand syntax for defining the anonymous function, i.e., for function expressions. These arrow functions are also called lambda functions. A lambda function is a function without a name. Whereas, the arrow function omits the function keyword.

What is Compilation Context?

View answer

The compilation context is basically grouping of the files that TypeScript will parse and analyze to determine what is valid and what isn’t. Along with the information about which files, the compilation context contains information about which compiler options. A great way to define this logical grouping is using a tsconfig.json file.

What is Triple-Slash Directive?

View answer

Triple-slash directives are single-line comments containing a single XML tag. The contents of the comment are used as compiler directives.

Why TypeScript is referred as Optionally Statically Typed Language?

View answer

TypeScript is referred as optionally statically typed, which means we can make the compiler to ignore the type of a variable optionally. Using "any" data type, we can assign any type of value to the variable. TypeScript will not give any error checking during compilation.

How to Call Base Class Constructor from Child Class in TypeScript?

View answer

super() function is used to called parent or base class constructor from Child Class.

What is default visibility for properties/methods in TypeScript classes?

View answer

Public is the default visibility for properties/methods in TypeScript classes.

Does TypeScript support all object-oriented principles?

View answer

Yes, TypeScript support all object-oriented principles. There are four main principles to object-oriented programming:

  • Encapsulation
  • Inheritance
  • Abstraction
  • Polymorphism

What is Type assertions in TypeScript?

View answer

Type assertion works like a typecasting in other languages, but it doesn't perform type checking or restructuring of data just like other languages can do like C# and Java. The typecasting comes with runtime support whereas type assertion has no impact on runtime. However, type assertions are used purely by the compiler and provide hints to the compiler on how we want our code to be analyzed.

What is Rest parameters?

View answer

The rest parameter is used to pass zero or more values to a function. It is declared by prefixing the three dot characters ('...')before the parameter. It allows the functions to have a variable number of arguments without using the arguments object. It is very useful where we have an undetermined number of parameters.

What is Declaration Merging?

View answer

Declaration merging is the process followed by the compiler to merge two or more separate declarations. The declaration declared with the same name into a single definition. This merged definition has the features of both of the original declarations.

The simplest, and perhaps most common, type of declaration merging is interface merging. At the most basic level, the merge mechanically joins the members of both declarations into a single interface with the same name.

What is getters/setters in TypeScript?

View answer

TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. This gives you a way of having finer-grained control over how a member is accessed on each object.

What is duck typing in TypeScript?

View answer

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.

What is an implicit Module in Typescript?

View answer

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.

What are TypeScript Optional Properties?

View answer

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.

What are Closures in Javascript?

View answer

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.

What is AMD in TypeScript?

View answer

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 the advantage of Arrow function in TypeScript?

View answer

The two significant benefits that Arrow function in TypeScript offers:

  • They are less verbose compared to traditional functions.
  • This function takes the “this” from its surroundings.

Why is TypeScript strongly typed?

View answer

TypeScript is strongly typed to syntactical benefits to this language while still allowing users to write normal JavaScript. It also ensures the production of predictable results with fewer errors and more secure.

What is static typing in TypeScript?

View answer

In TypeScript, static typing means parameters, variables, and object members have types that compiler recognizes at the time of compile. This helps in detecting early errors faster than unit test runs. It also greatly helps IDEs with auto-completion and API exploration with statically typed DOM.

What is the prototype in TypeScript?

View answer

The prototype property in TypeScript allows users to include methods and properties to an object. It allows cloning objects (complex ones also) without coupling to their specific classes. Prototype objects can create full copies due to the accessibility of objects to each other’s private field.

What is the callback function in TypeScript?

View answer

In TypeScript, the callback function is used to schedule a call after some asynchronous processing is completed. It is passed to another function as a parameter that allows callback function to be called when the process of asynchronous processing is completed.

Explain access modifiers in TypeScript?

View answer

TypeScript developers can use the access modifiers to control the accessibility of the class members. There are two types of access modifiers in TypeScript; private and public modifiers. Class members are public by default, but users can add a private modifier if they wish.

What is super in TypeScript?

View answer

Super is a TypeScript keyword which can be used by developers in expressions for base class constructor and base class properties reference. This call is only allowed in constructors of derived classes. Call for this keyword consist the keyword super with an argument list closed in parentheses.

What is the export class in TypeScript?

View answer

In TypeScript, an extra line is added to the JS code using the export class which is then used to add an exported item within a module.

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

View answer

Yes, it's possible. To successfully merge multiple TS files together in a JS file, use a module bundler system or a Gulp Script to concatenate everything into a single JS file.

What is import and export in TypeScript?

View answer

The export keyword is used to export any declaration such as function, variables, interface, or type alias. The export * from "module" can be used by a module to wrap one or more modules and combine together their exporting. Whereas, the import keyword is used to export declaration.

Which keyword is used for inheritance in TypeScript?

View answer

TypeScript uses the keyword ‘extends’ to support inheritance. The syntax will be as following:

class child_class_name extends parent_class_name

What does the ‘Omit’ type do?

View answer

It’s a new type where you can define some properties to be excluded from the original type.

Where can decorators be applied to?

View answer

Classes, properties, methods, and method arguments.

What does the ‘Record’ type do?

View answer

It allows you to create a typed map .

How can you access classes outside the module they are defined in?

View answer

Use the export keyword in front of the class.

When to use the ‘unknown’ keyword?

View answer

When you don’t want to use the "any" keyword or/and don’t know the exact type up front but want to assign it later on.

What is the difference between var and let in Typescript?

View answer

1.var declaration is done at function scoped and let declaration is done at block scoped. 2.var will allow you to re-declare the same variable in the same scope, whereas let will not.

What are methods overriding in Typescript?

View answer

If child class has the same method as declared in the parent class, it is known as method overriding.

Rules for Method Overriding:

  • The method must have the same name as in the parent class
  • There must be an IS-A relationship
  • The method must have the same parameter as in the parent class

How to check null and undefined in typescript?

View answer

By using a juggling check, we can check both null and undefined: if(x==null){}.

What does it mean by Semantic HTML?

View answer

Semantic HTML is a type of coding style in which tags will have texts that need to display on the user interface. Bold, Italic tags should not be used to represent Semantic HTML.

List out all the components of TypeScript.

View answer

Below are the components of TypeScript :-

  • Language
  • TSC-(TypeScript Compiler)
  • TSLS- (TypeScript Language Service)

What are the file Naming Extension’s in TypeScript?

View answer

There are two extension’s – .tsx , .ts.

What is tuple in TypeScript?

View answer

Tuple is collection of heterogeneous values or it store multiple different-different type data type values. Below is the example for the same.

var fileData = [“Alia”,90,”Shweta”,45];

Describe pop() and push() method in tuple.

View answer

  • push() – this used to append the value.

  • pop()- this used to removed and return the last value.

What are the different types supported by TypeScript ?

View answer

  • 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.

What are TypeScript collections ?

View answer

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

Is it possible to debug any TypeScript file?

View answer

Yes, it is possible. To debug it, you need the .js source map file. So compile the .ts file with the --sourcemap flag to generate a source map file. tsc -sourcemap file1.ts This will create file1.js and file1.js.map. And last line of file1.js would be reference of source map file.


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

View answer

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.

How do you declare an undefined variable?

View answer

var name:string; var phone:int;

How to declare and initialize an array in Typescript ?

View answer

var names:string[]; -- Declaration names=["a","b","c","d"] -- Initialization

What is union type ?

View answer

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.

How to pass options to TypeScript compiler?

View answer

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.

What does the pipe(|) mean in typescript?

View answer

This is called union type in typescript.

A union type describes a value that can be one of several types.