Google News
logo
TypeScript Interview Questions
We know, TypeScript is a type of Object-Oriented JavaScript language and supports OOPs programming features like classes, interfaces, etc. Like Java, classes are the fundamental entities which are used to create reusable components. It is a group of objects which have common properties. A class is a template or blueprint for creating objects. It is a logical entity. The "class" keyword is used to declare a class in Typescript.
 
Example :
class Student {    
    studCode: number;    
    studName: string;    
    constructor(code: number, name: string) {    
            this.studName = name;    
            this.studCode = code;    
    }    
    getGrade() : string {    
        return "A+" ;    
    }    
}​
    
Features of a class are :
 
* Inheritance
* Encapsulation
* Polymorphism
* Abstraction
 The access modifiers supported by TypeScript are :
 
Protected : All child classes and members have access to them, but the instance does not.
Private : Only members have access
Public : Members, child classes, and instances of the class have access to the public modifier.
A loop statement allows to repeat the execution of a statement or a series of statements. To handle looping requirements, TypeScript provides a variety of loops.
 
* For Loop : The for loop is a definite loop implementation. A definite loop is defined as a loop with a fixed number of iterations.
 
* While Loop : When the condition supplied evaluates to true, the while loop executes the instructions.
 
* Do..While Loop : The do...while loop is identical to the while loop, except that the condition isn't evaluated the first time the loop runs.
When one side of the equation has type but the other does not, the TypeScript compiler determines the form. You can skip typing on the right side because TypeScript will figure it out for you. This reduces the amount of time spent typing code.
Static typing refers to a compiler that has recognisable variables, arguments, and object members at compile time. This aids in the early detection of faults.
* Import keyword is used to export declaration. Example: export * from module
* Any variable, function, or type alias can be exported with the export keyword
By combining TypeScript with Node.js, backend applications can benefit from the added security that the language provides.
if(value) return true if value is not null, undefined, empty, false, 0 or NaN.
A module is a powerful way to create a group of related variables, functions, classes, and interfaces, etc. It can be executed within their own scope, not in the global scope. In other words, the variables, functions, classes, and interfaces declared in a module cannot be accessible outside the module directly.
 
Creating a Module
 
A module can be created by using the export keyword and can be used in other modules by using the import keyword.
module module_name{  
    class xyz{  
        export sum(x, y){  
            return x+y;  
         }  
    } ​
We can divide the module into two categories :
 
* Internal Module
* External Module
 
Internal Module : Internal modules were in the earlier version of Typescript. It was used for logical grouping of the classes, interfaces, functions, variables into a single unit and can be exported in another module. The modules are named as a namespace in the latest version of TypeScript. So today, internal modules are obsolete. But they are still supported by using namespace over internal modules.
 
Syntax :
module Sum {   
   export function add(a, b) {    
      console.log("Sum: " +(a+b));   
   }   
} ​

 

External Module : External modules are also known as a module. When the applications consisting of hundreds of files, then it is almost impossible to handle these files without a modular approach. External Module is used to specify the load dependencies between the multiple external js files. If the application has only one js file, the external module is not relevant. ECMAScript 2015(ES6) module system treats every file as a module.

Syntax :
export class MyClass {
}
export var x:MyClass instance = new MyClass();
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.
 
Syntax :
namespace <namespace_name> {
export interface I1 { }
export class c1{ }