Dear Readers, Welcome to TypeScript Interview Questions and Answers have been designed specially to get you acquainted with the nature of questions you may encounter during your Job interview for the subject of TypeScript Interview Questions. These TypeScript Questions are very important for campus placement test and job interviews. As per my experience good interviewers hardly plan to ask any particular questions during your Job interview and these model questions are asked in the online technical test and interview of many IT & Non IT Industries.
TypeScript is a free and open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing and class-based object-oriented programming to the language.
TypeScript lets you write JavaScript the way you really want to. TypeScript is pure object oriented like Java. TypeScript can help programmers to write object-oriented programs and have them compiled to JavaScript, both on server side and client side.
Modules in Typescripts are very useful. Modules in Typescript helps in organizing the code.
There are 2 types of Modules:
i. Internal
ii. External
=> Internal Modules are now replaceable by using Typescript’s namespace.
=> External Modules - used to specify and load dependencies between multiple external js files. If there is only one js file used, then external modules are not relevant.
Typescript is a free and open-source programming language which is designed and developed by Microsoft. It was designed by Anders Hejlsberg at Microsoft. Typescript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. Typescript is compiled to provide clean and simple JavaScript code which runs on any browser. It allows JavaScript developers for using highly productive development tools and practices like static checking and code refactoring.
Differences between Typescript and JavaScript are:
JavaScript don’t support Es6 while Typescript supports .
JavaScript build up reusable components by using unctions and prototype-based inheritance while Typescript supports Classes that allow programmer to think in more object oriented way .
JavaScript don’t have any interfaces while Typescript has interfaces.
There is no static typing in JavaScript whereas there is static typing in Typescript.
JavaScript has no optional parameter feature while Typescript has optional parameter feature.
Features of Typescript are:-
Typescript can be compiled to all major versions of Javascript(ES3,ES5,ES6,ES7).
Typescript can be used for cross –browser development and is an open source project.
Typescript is a superset of JavaScript that provides typed nature to your code.
It is used to avoid the disadvantages of JavaScript like Typescript is designed to handle the needs of complex programs.
Typescript was debuted as a beta programming language on October 1, 2012 and since then has gone through many versions with improved capabilities.
Another key feature of Typescript is in version Typescript 2.6 that covers error suppression comments.
Typescript is more predictable and is generally easier to debug.
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.
The answer is YES.
There are 4 main principles to Object Oriented Programming:
=> Encapsulation,
=> Inheritance,
=> Abstraction, and
=> Polymorphism.
TypeScript can implement all four of them with its smaller and cleaner syntax.
TypeScript supports following object oriented terms.
=> Modules
=> Classes
=> Interfaces
=> Data Types
=> Member functions
if (value) {
}
It will evaluate to true if value is not:
. null
. undefined
. NaN
. empty string ''
. 0
. false
TypesScript includes JavaScript rules.
There are mainly 3 components of TypeScript :
Language – The most important part for developers is the new language. The language consist of new syntax, keywords and allows you to write TypeScript.
Compiler – The TypeScript compiler is open source, cross-platform and open specification, and is written in TypeScript. Compiler will compile your TypeScript into JavaScript. And it will also emit error, if any. It can also help in concating different files to single output file and in generating source maps.
Language Service – TypeScript language service which powers the interactive TypeScript experience in Visual Studio, VS Code, Sublime, the TypeScript playground and other editor.
The concept of classes is very similar to .Net/Java. A Class can have constructor, member variables, properties and methods. TypeScript also allows access modifiers “private” and “public” for member variables and functions.
Using super(), we can call base class constructor, as seen in above code.
In TypeScript, each member of class is public by default.
Well, TypeScript is great but there are some disadvantage as well.
=> TypeScript is just another way to write JavaScript. It doesn’t fix the problems of JavaScript. It just creates an illusion that it does.
One more tool to learn.
=> TypeScript has dependency on type definition files, if you wish to use any existing JavaScript libraries.
=> Quality of type definition files is a concern as how can you be sure the definitions are correct?
=> Frequent releases of new versions JavaScript library is also a pain area. Because if their type definition files are not updated then you can’t use them instantly.
=> In order to run the application in the browser, a compile step is required to transform TypeScript into JavaScript.
=> Web developers are using JavaScript from decades and TypeScript doesn’t bring anything new.
=> To use any third party library, definition file is you need. And not all the third party library have definition file available.
These are also called the primitive types in Typescript.
These are of 5 types:
Number type: it is used to represent number type values and represents double precision floating point values.
Syntax- var variable_name: number;
String type: it represents a sequence of characters stored as Unicode UTF-16 code. It is the same as JavaScript primitive type.
Syntax- var variable_name: string;
Boolean type: in Typescript, it is used to represent a logical value. When we use the Boolean type, we get output only in true or false. It is also the same as JavaScript primitive type.
Syntax- var variable_name: bool;
Null type: it represents a null literal and it is not possible to directly reference the null type value itself.
Syntax- var variable_name:number=null;
Undefined type: it is the type of undefined literal. This type of built-in type is the sub-type of all the types.
Syntax- var variable_name:number=undefined;
An interface is a syntactical contract which defines the syntax of any entity. Interfaces in Typescript define properties, methods, and events which are the members of the interface. It contains only the declaration of the members. It is responsible for deriving classes to define the members. It often helps in providing a standard structure that the deriving classes would follow. For declaring an interface, we make use of the “interface” keyword.
Syntax :-
interface interface_name{
Statements;
}
Interfaces need not to be converted to JavaScript for execution. They have zero runtime JavaScript impact. Interfaces can define both the kind of keys which an array uses and the type of entry it contains.
A Typescript file is used to write more manageable and complex code in AppBuilder but before that, they need to be compiled to JavaScript. Here are the steps which are followed while compiling a Typescript file to JavaScript: –
It should be firstly verified that the Typescript engine has been enabled or not. In the title bar, click your username and select options.
In the project navigator, select and right-click the TS files that are to be compiled.
Choose compile to JavaScript.
Add a <script> reference for the JS file in the HTML code if needed.
To compile a typescript file via command line tsc <TypeScript File Name> command is used.
Modules are powerful way to share code between files.By using modules effectively in your project you can keep you file size small and dependencies clear.
Modules are executed within their own scope, not in the global scope; this means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms.
Creating a Module
module module_name{
class xyz{
export foo(x, y){
return x*y;
}
}
}
The extension for any TypeScript file is “.ts”. And any JavaScript file is TypeScript file as it is a superset of JavaScript. So change extension of “.js” to “.ts” file and your TypeScript file is ready. To compile any .ts file into .js, use the following command.
tsc <TypeScript File Name>
For example, to compile “Helloworld.ts”:
tsc helloworld.ts
And the result would be helloworld.js.
TypeScript supports the following data types:
Boolean var bValue: boolean = false;
Number var age: number = 16;
String var name: string = "jon";
Array var list:number[] = [1, 2, 3];
Enum
enum Color {Red, Green, Blue};
var c: Color = Color.Green;
Any var unknownType: any = 4;
Void
function NoReturnType(): void {
}
Using extends keyword, we can implement inheritance.
class Animal {
public domestic:boolean;
constructor(public name: string) { }
}
class Cat extends Animal {
constructor(name: string, domestic: boolean)
{
super(name);
this.domestic = true;
}
}
class Tiger extends Animal {
constructor(name: string, domestic: boolean)
{
super(name);
this.domestic = false;
}
}