Google News
logo
TypeScript Interview Questions
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.
Type assertion works like a typecasting in other languages, but it doesn’t perform type checking or restructuring of data in other languages 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.
 
Example :
let empCode: any = 007;
let employeeCode = <number> code;
console.log(typeof(employeeCode)); //Output: number
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.
Rules to follow in rest parameter :
 
* Only one rest parameter is allowed in a function.
* It must be an array type.
* It must be a last parameter in the parameter list.

Example :
function sum(a: number, ...b: number[]): number {
let result = a;
for (var i = 0; i < b.length; i++) {
result += b[i];
}
console.log(result);
}
let result1 = sum(2, 4);
let result2 = sum(2,4,6,8);
Yes, TypeScript support all object-oriented principles. There are four main principles to object-oriented programming :
 
* Encapsulation,
* Inheritance,
* Abstraction, and
* Polymorphism.
By using a juggling-check, we can check both null and undefined :
if (x == null) {  
} ​
 If we use a strict-check, it will always true for values set to null and won't evaluate as true for undefined variables.
 
Example :
var a: number;  
var b: number = null;  
function check(x, name) {  
    if (x == null) {  
        console.log(name + ' == null');  
    }  
    if (x === null) {  
        console.log(name + ' === null');  
    }  
    if (typeof x === 'undefined') {  
        console.log(name + ' is undefined');  
    }  
}  
check(a, 'a');  
check(b, 'b');​
Output :
"a == null"  
"a is undefined"  
"b == null"  
"b === null"  
The as is the additional syntax for Type assertion in TypeScript. The reason for introducing the as-syntax is that the original syntax (<type>) conflicted with JSX.
 
Example :
let empCode: any = 111;     
let employeeCode = code as number;​
When using TypeScript with JSX, only as-style assertions are allowed.
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.
 
Example :
let myAdd = function(x: number, y: number): number { 
return a+b; 
}; 
console.log(myAdd())
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding. In other words, redefined the base class methods in the derived class or child class.
 
Rules for Method Overriding
 
* The method must have the same name as in the parent class
* The method must have the same parameter as in the parent class.
* There must be an IS-A relationship (inheritance).

Example :
class NewPrinter extends Printer {  
    doPrint(): any {  
        super.doPrint();  
        console.log("Called Child class.");  
    }  
    doInkJetPrint(): any {  
        console.log("Called doInkJetPrint().");  
    }  
}  
let printer: new () => NewPrinter;  
printer.doPrint();  
printer.doInkJetPrint(); 
Tuples are a collection of values that are diverse. It allows for the storage of many fields of various sorts. Tuples can also be used as function parameters.
 
There are instances when it is necessary to save a collection of values of various types. Arrays will not suffice in this situation. TypeScript provides a data type called tuple that aids in this endeavor.
 
Syntax :
var tuple_name = [value c,value b,value c,…value n]
For Example :
var yourtuple = [12,"Hi"];