TypeScript is an extension of JavaScript and is a strongly typed, object-oriented programming language developed by Microsoft, with Anders Hejlsberg as the lead architect. It is an open-source language that serves as a superset of JavaScript, adding optional static types and other features to JavaScript. TypeScript code is compiled into JavaScript, allowing it to run in any environment where JavaScript is supported.
If you are looking to interview for a developer role specializing in TypeScript, the following TypeScript interview questions and answers will help you cover all the possible questions that may come your way.
TypeScript is a superset of JavaScript. It is an object-oriented programming language. Compared to JavaScript, TypeScript code is a tightly typed programming language that may be used in any environment that supports JavaScript, including browsers, Node.js, and your applications.
TypeScript is a variant of JavaScript with some more features. TypeScript extends JavaScript with extra syntax to provide a more robust interface with your editor. TypeScript is a scripting language compatible with JavaScript and relies on type inference to deliver advanced functionality without additional code.
Problems are highlighted throughout development and at compilation time.
Typescript can be run in any browser or JavaScript engine.
A namespace notion is created by declaring a module.
IntelliSense is a TypeScript feature that provides active hints as you type.
Strongly typed or static typing is supported. The advantage of TypeScript is that it is strictly typed or allows for static typing. Because of static typing, it may confirm type correctness at compilation time.
It takes a long time to compile TypeScript code.
Abstract classes are not supported in TypeScript.
Converting TypeScript to JavaScript requires an additional compilation step.
Its type scheme is extremely complicated.
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 an index of 0, the second has an index of 1, and so on.
Here is the syntax to declare and initialize an array in TypeScript.
let values: numberl] = 0; 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 = [15, 20, 25, 30]
Let and const are the two main ways to declare variables.
A digit can’t be the first character in a variable name.
let var a=10;
OR
function f() { var message = "Hello, world!"; return message; }
The access modifiers supported by TypeScript are three types:
There are 3 ways to declare variables in typescript it is almost similar to JavaScript
Var: Declares a function-scoped or global variable. You can optionally set its value during the declaration. Its behaviour and scoping rules are similar to the var keyword in JavaScript. For example,
var a = "apple";
let: Declares a block-scoped local variable. Similar to var, you can optionally set the value of a variable during the declaration. Its scope is only inside the block. For example,
let a = 5: if (true) { let a = 10; console.log(a); // 10 }
const: Declares a block-scoped constant value that cannot be changed after initialisation. For example,
Const a =5; if (true) { a = 10; // Error: Cannot assign to 'a' because it is a constant.(2588) } console.log(a);
A lambda function is a small anonymous function defined inline with larger expressions; it's also known as an arrow function because its symbol resembles arrow function syntax.
Example:
let sum=(a: num, b: num): num=>{ return a+b;}
console.log(sum(5,10)); //returns 15
Here, ?=>? is a lambda operator.
Objects are collections of keys and values that resemble a dictionary. The keys must be one-of-a-kind. They look like arrays and are sometimes referred to as associative arrays. On the other hand, an array employs numbers to index the values, whereas an object lets you use any type as the key.
An Object type in TypeScript points to any value with properties. It can be defined simply by specifying the properties and the kinds of those properties. As an example,
let pt: { x: number; y: number } = {
x: 10,
y: 20
};
TypeScript has three primitive types that are frequently used: string, number, and boolean. These correspond to the similarly named types in JavaScript.
Tuples are a special type in TypeScript. They are similar to arrays with a fixed number of elements with a known type. However, the types need not be the same.
// Declare a tuple type and initialize it let values: [string, number] = ["Foo", 15]; // Type boolean' is not assignable to type ‘string’.(2322) // Type 'string' is not assignable to type ‘number’.(2322) let wrongValues: [string, number] = [true,"hello"}:
Since TypeScript 3.0, a tuple can specify one or more optional types using the ? as shown below.
let values: [string, number, boolean?] = ["Foo",15];