Google News
logo
TypeScript Interview Questions
Yes, we can combine multiple files. While compiling, we need to add –outFILE [OutputJSFileName] option.
 
tsc -- outFile comman.js file1.ts file2.ts file3.ts
 
This will compile all 3 “.ts” file and output into a single “comman.js” file.
 
tsc --outFile file1.ts file2.ts file3.ts
 
If you don’t provide an output file name, file2.ts and file3.ts will be compiled and the output will be placed in file1.ts. So now your file1.ts contains JavaScript code.
The Type System represents the different types of values supported by the language. It checks the validity of the supplied values before they are stored or manipulated by the program.
 
It can be classified into two types such as :
 
Built-in : This includes number, string, boolean, void, null and undefined.
User-defined : It includes Enumerations (enums), classes, interfaces, arrays, and tuple.
The built-in data types are also known as primitive data types in Typescript. These are given below.
 
Number type : It is used to represent number type values. All the numbers in TypeScript are stored as floating point values.
Syntax : let identifier: number = value;
 
String type : It represents a sequence of characters stored as Unicode UTF-16 code. We include string literals in our scripts by enclosing them in single or double quotation marks.
Syntax : let identifier: string = " ";
 
Boolean type : It is used to represent a logical value. When we use the Boolean type, we get output only in true or false. A Boolean value is a truth value that specifies whether the condition is true or not.
Syntax : let identifier: bool = Boolean value;
 
Null type : Null represents a variable whose value is undefined. It is not possible to directly reference the null type value itself. Null type is not useful because we can only assign a null value to it.
Syntax : let num: number = null;
 
Undefined type : It is the type of undefined literal. The Undefined type denotes all uninitialized variables. It is not useful because we can only assign an undefined value to it. This type of built-in type is the sub-type of all the types.
Syntax : let num: number = undefined;
 
Void type : A void is the return type of the functions that do not return any type of value. It is used where no datatype is available.
Syntax : let unusable: void = undefined;
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. Similar to JavaScript, we use the var keyword to declare a variable. While declaring a variable in Typescript, certain rules must be followed-
 
* The variable name must be an alphabet or numeric digits.
* You cannot start the name with digits.
* It cannot contain spaces and special characters, except the underscore(_) and the dollar($) sign.
There are four ways of declaring a variable :
var [identifier] : [type-annotation] = value; //Declaring type and value in a single statement

var [identifier] : [type-annotation]; //Declaring type without value

var [identifier] = value; //Declaring its value without type

var [identifier]; //Declaring without value and type
We use arrays to store values of the same type. Arrays are ordered and indexed collections of values. The indexing starts at 0, i.e., the first element has index 0, the second has index 1, and so on.
 
Here is the syntax to declare and initialize an array in TypeScript.
let values: number[] = [];
values[0] = 10;
values[1] = 20;
values[2] = 30;
You can also create an array using the short-hand syntax as follows :
let values: number[] = [15, 20, 25, 30];
TypeScript provides an alternate syntax to specify the Array type.
let values: Array<number> = [15, 20, 25, 30];
There are times when you want to store a value in a variable but don’t know the type of that variable in advance. For example, the value is coming from an API call or the user input. The ‘any’ type allows you to assign a value of any type to the variable of type any.
let person: any = "Foo";
Here is an example that demonstrates the usage of any type.
// json may come from a third-party API
const employeeData: string = `{"name": "Ramana", "salary": 90000}`;

// parse JSON to build employee object
const employee: any = JSON.parse(employeeData);

console.log(employee.name);
console.log(employee.salary);
TypeScript assumes a variable is of type any when you don’t explicitly provide the type, and the compiler cannot infer the type from the surrounding context. 
The void indicates the absence of type on a variable. It acts as the opposite type to any. It is especially useful in functions that don’t return a value.
function notify(): void {
  alert("The user has been notified.");
}
If a variable is of type void, you can only assign the null or undefined values to that variable.
TypeScript supports the following object-oriented terms :
 
* Modules
* Classes
* Interfaces
* Inheritance
* Data Types
* Member functions
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.
 
Syntax :
interface interface_name {
// variables' declaration
// methods' declaration
}