Top 25 Typescript Interview Questions and Answers (updated 2019)
Q1. What is TypeScript and Why Do We Need It?
Ans. JavaScript is the most effective customer side language universally supported by way of all browsers. But JavaScript isn't always the pleasant designed language. It’s now not a class-primarily based item-oriented language, doesn’t support class based inheritance, unreliable dynamic typing and lacks in compile time blunders checking. And TypeScript addresses these types of troubles. In different words, TypeScript is an try to “restoration” JavaScript troubles.
TypeScript is a unfastened and open supply programming language developed and maintained through Microsoft. It is a strict superset of JavaScript, and provides optionally available static typing and sophistication-primarily based item-oriented programming to the language. TypeScript is quite clean to analyze and use for builders familiar with C#, Java and all strong typed languages. At the stop of day “TypeScript is a language that generates undeniable JavaScript files.”
As said on Typescript reliable internet site, “TypeScript helps you to write JavaScript the manner you actually need to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS. Open Source.” Where “typed” method that it considers the varieties of variables, parameters and capabilities.
Q2. What are the Benefits of TypeScript?
Ans. TypeScript has following benefits:
It facilitates in code structuring
Use magnificence based totally item oriented programming
Impose coding tips
Offers type checking
Compile time mistakes checking
Intellisense
Q3. What are Different Components of TypeScript?
Ans. There are mainly 3 components of TypeScript:
Language– The maximum crucial part for developers is the new language. The language consists of recent syntax, key phrases and permits you to jot down TypeScript.
Compiler– The TypeScript compiler is open source, move-platform and open specification, and is written in TypeScript. Compiler will compile your TypeScript into JavaScript. And it will also emit errors, if any. It also can help in concatenating unique documents to a single output report and in producing supply maps.
Language Service– TypeScript language provider which powers the interactive TypeScript experience in Visual Studio, VS Code, Sublime, the TypeScript playground and other editor.
Q4. How Can You Get TypeScript and Install It?
Ans. TypeScript can be mounted and managed through npm, the Node.Js bundle manager. To deploy TypeScript, first make certain the npm is set up nicely. And then run the subsequent command to put in TypeScript globally for your machine.
Hide Copy Code
npm installation -g typescript
TypeScript is blanketed in Visual Studio 2013 Update 2 and Visual Studio 2015 through default. TypeScript also provides support for other editors like Visual Studio Code, chic, Emacs and Vim.
Q5. How Do You Compile TypeScript Files?
Ans. The extension for any TypeScript record is “.Ts”. And any JavaScript record is TypeScript report as it is a superset of JavaScript. So trade extension of “.Js” to “.Ts” record and your TypeScript document is prepared. To assemble any .Ts record into .Js, use the subsequent command.
Hide Copy Code
tsc <TypeScript File Name>
For example, to compile “Helloworld.Ts”:
Hide Copy Code
tsc helloworld.Ts
And the end result would be helloworld.Js.
Q6. Is It Possible to Combine Multiple .Ts Files right into a Single .Js File?
Ans. Yes, it's possible. While compiling add --outFILE [OutputJSFileName] choice.
Hide Copy Code
tsc --outFile comman.Js file1.Ts file2.Ts file3.Ts
This will compile all 3 “.Ts” document and output into unmarried “comman.Js” record. And what is going to occur in case you don’t provide a output document name.
Hide Copy Code
tsc --outFile file1.Ts file2.Ts file3.Ts
In this example, file2.Ts and file3.Ts might be compiled and the output may be placed in file1.Ts. So now your file1.Ts contains JavaScript code.
Q7. Is It Possible to Compile .Ts Automatically with Real Time Changes in .Ts File?
Ans. Yes. Using --watch compiler alternative, this may be achieved.
Hide Copy Code
tsc --watch file1.Ts
This will first bring together file1.Ts in record.Js and look ahead to the file changes. As soon as there's any exchange detected, it's going to bring together it once more. But you need to ensure that command spark off have to now not be closed, used with --watchoption.
Q8. Does TypeScript Support All Object Oriented Principles?
Ans. The answer is YES. There are four principal concepts to Object Oriented Programming: Encapsulation, Inheritance, Abstraction, and Polymorphism. TypeScript can enforce all four of them with its smaller and purifier syntax. Read Write Object-Oriented JavaScript with TypeScript.
Q9. Which Object Oriented Terms are Supported by using TypeScript?
Ans. TypeScript helps the subsequent item orientated phrases:
Modules
Classes
Interfaces
Data Types
Member capabilities
Q10. Which are the Different Data Types Supported by way of TypeScript?
Ans. TypeScript helps the following records sorts:
Booleanvar bValue: boolean = fake;
Numbervar age: wide variety = sixteen;
Stringvar call: string = "jon";
Arrayvar list:number[] = [1, 2, 3];
Enum
Hide Copy Code
enum Color Red, Green, Blue;
var c: Color = Color.Green;
Anyvar unknownType: any = 4;
Void
Hide Copy Code
function NoReturnType(): void
Q11. How TypeScript is Optionally Statically Typed Language?
Ans. TypeScript is referred as optionally statically typed, which means that you could ask the compiler to ignore the sort of a variable. Using any statistics type, we are able to assign any kind of value to the variable. TypeScript will now not provide any blunders checking at some stage in compilation.
Hide Copy Code
var unknownType: any = 4;
unknownType = "Okay, I am a string";
unknownType = false; // A boolean.
Q12. What are Modules in TypeScript?
Ans. Modules are the manner to prepare code in TypeScript. Modules don’t have any functions, but can incorporate lessons and interfaces. It is same like namespace in C#.
Q13. What are Classes in TypeScript?
Ans. The concept of classes could be very similar to .NET/Java. A Class will have constructor, member variables, residences and strategies. TypeScript additionally allows access modifiers “non-public” and “public” for member variables and functions.
Q14. How Do You Implement Inheritance in TypeScript?
Ans. Using extends keyword, we can put into effect inheritance.
Hide Copy Code
magnificence Animal
public home:boolean;
constructor(public name: string)
magnificence Cat extends Animal
constructor(name: string, domestic: boolean)
fantastic(call);
this.Home = genuine;
elegance Tiger extends Animal
constructor(name: string, domestic: boolean)
amazing(name);
this.Domestic = false;
Q15. How to Call Base Class Constructor from Child Class in TypeScript?
Ans. Using amazing(), we will call base class constructor, as seen within the above code.
Q16. What is the Default Access Modifier for Members of a Class in TypeScript?
Ans. In TypeScript, each member of sophistication is public with the aid of default.
Q17. How Can You Allow Classes Defined in a Module to be Accessible Outside of the Module?
Ans. Classed define in a module are available in the module. Outside the module, you could’t get right of entry to them.
Hide Copy Code
module Vehicle
magnificence Car
constructor (
public make: string,
public version: string)
var audiCar = new Car("Audi", "Q7");
// This might not paintings
var fordCar = Vehicle.Car("Ford", "Figo");
As in line with above code, fordCar variable will deliver us bring together time errors. To make instructions accessible out of doors module, use export key-word for training.
Hide Copy Code
module Vehicle
export class Car
constructor (
public make: string,
public version: string)
var audiCar = new Car("Audi", "Q7");
// This works now
var fordCar = Vehicle.Car("Ford", "Figo");
Q18. How Does TypeScript Support Optional Parameters in Function as in JavaScript Every Parameter is Optional for a Function?
Ans. In JavaScript, each parameter is considered non-compulsory. If no cost is provided, then it's far handled as undefined. So whilst writing functions in TypeScript, we will make a parameter elective the usage of the “?” after the parameter name.
Hide Copy Code
characteristic Demo(arg1: variety, arg2? :variety)
So, arg1 is usually required and arg2 is an optionally available parameter. Remember, non-compulsory parameters need to comply with required parameters. If we need to make arg1 optional, in place of arg2, then we need to trade the order and arg1 must be put after arg2.
Hide Copy Code
function Demo(arg2: range, arg1? :wide variety)
Similar to optional parameters, default parameters also are supported.
Hide Copy Code
characteristic Demo(arg1: variety, arg2 = 4)
Q19. Does TypeScript Support Function Overloading as JavaScript Doesn’t Support Function Overloading?
Ans. Yes, TypeScript does help function overloading. But the implementation is strange. When you overload in TypeScript, you most effective have one implementation with multiple signatures.
Hide Copy Code
elegance Customer
call: string;
Id: range;
upload(Id: variety);
upload(call:string);
add(price: any)
if (fee && typeof cost == "range")
//Do something
if (value && typeof fee == "string")
//Do Something
The first signature has one parameter of kind number whereas the second signature has a parameter of type string. The 1/3 characteristic incorporates the real implementation and has a parameter of kind any. The any data kind can take any sort of records. The implementation then checks for the form of the provided parameter and executes a exceptional piece of code primarily based on provider parameter type.
OR You can also use union kind brought in TypeScript 1.Four. Union sorts will let you represent a value that is one among a couple of sorts.
Hide Copy Code
add(a:stringquantity)
//do something
Using union type, you may generally remove the need for an overload.
Q20. Is It Possible to Debug Any TypeScript File?
Ans. Yes, it is possible. To debug it, you need .Js source map report. If you are new to source map, examine greater right here. So collect the .Ts file with the --sourcemap flag to generate a source map report.
Hide Copy Code
tsc -sourcemap file1.Ts
This will create file1.Js and file1.Js.Map. And final line of file1.Js might be reference of source map file.
Hide Copy Code
//# sourceMappingURL=file1.Js.MapQ21. What is TypeScript Definition Manager and Why Do You Need It?
Ans. TypeScript Definition Manager (TSD) is a package manager to go looking and installation TypeScript definition documents at once from the community driven DefinitelyTyped repository. Let’s see with an instance.
Suppose, you want to use some jQuery code for your .Ts file.
Hide Copy Code
$(document).Geared up(characteristic() //Your jQuery code );
And now whilst you try to bring together it using tsc, you'll get bring together time blunders Cannot find call “$”. That’s because TypeScript can’t apprehend what does “$” approach. So somehow we want to tell TypeScript compiler that it belongs to jQuery. That’s wherein TSD comes into play. You can download jQuery Type Definition document and encompass it in your .Ts document. First, installation TSD.
Hide Copy Code
npm deploy tsd -g
In your typescript listing, create a brand new typescript task by jogging:
Hide Copy Code
tsd init
Then installation the definition file for jquery.
Hide Copy Code
tsd question jquery --motion install
This will download and create a brand new listing containing your jquery definition file. The definition report ends with “.D.Ts”. So now encompass it by way of updating your typescript file to factor to the jquery definition.
Hide Copy Code
/// <reference path="typings/jquery/jquery.D.Ts" />
$(document).Equipped(feature() //To Do
);
Now try to compile again and this time, js might be generated without any blunders.
Ans. It’s quite feasible that JavaScript libraries/frameworks don’t have TypeScript definition files and yet you need to use them with none errors. Te solution is to apply the declare key-word. The declare keyword is used for ambient declarations in which you want to define a variable that won't have originated from a TypeScript file.
Hide Copy Code
declare var unKnownLibrary;
TypeScript runtime will assign unKnownLibrary variable any kind. You gained’t get Intellisense in layout time however you will be able to use the library in your code.
Q23. How to Generate TypeScript Definition File from Any .Ts File?
Ans. You can generate TypeScript definition file from any .Ts file thru tsc compiler. Generating a TypeScript definition will make your TypeScript document reusable.
Hide Copy Code
tsc --announcement file1.Ts
Q24. What is tsconfig.Json File?
Ans. The presence of a tsconfig.Json file in a directory shows that the listing is the root of a TypeScript undertaking. The tsconfig.Json document specifies the basis documents and the compiler options required to bring together the assignment. And the use of this report, we can streamline constructing TypeScript mission. Below is a pattern tsconfig.Json file.
Hide Copy Code
"compilerOptions":
"removeComments": proper,
"sourceMap": proper
,
"documents": [
"main.Ts",
"othermodule.Ts"
]
Within files segment, define all the .Ts documents inside the assignment. And when you invoke tsc with none other arguments with the above file within the contemporary listing, it will compile all of the documents with the given compiler alternative settings.
Q25. What are the Disadvantages of TypeScript?
Ans. Well, TypeScript is excellent but there are a few disadvantages as well.
TypeScript is simply any other way to put in writing JavaScript. It doesn’t fix the problems of JavaScript. It just creates an phantasm that it does.
One extra device to research.
TypeScript has dependency on kind definition files, if you want to use any present JavaScript libraries.
Quality of type definition files is a difficulty as how are you going to be sure the definitions are accurate?
Frequent releases of new variations JavaScript library is likewise a ache vicinity. Because if their type definition files aren't up to date then you could’t use them right away.
In order to run the application within the browser, a bring together step is required to transform TypeScript into JavaScript.
Web developers are the usage of JavaScript from a long time and TypeScript doesn’t deliver something new.
To use any third celebration library, definition record is you want. And now not all the 1/3 celebration library have definition record available.
