Typescript Flashcards

1
Q

Explain Arrays in TypeScript

A

A collection of values of the same data type is called an array. It’s a kind that’s been defined by the user. To store values of the same kind, you use arrays. Arrays are collections of values that are ordered and indexed. The indexing begins at zero, with the first element having index 0, the second having index 1, and so on.

Syntax:

var array_name[:datatype]; //declaration

array_name = [val1,val2,valn..] //initialization

Example:

let values: number[] = [];

values[0] = 10;

values[1] = 20;

values[2] = 30;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List the Advantages of TypeScript

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List the disadvantages of TypeScript

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to declare a variable in TypeScript?

A

Let and const are the two methods to declare variables.
Alphabets and numeric digits can both be used in variable names.
Except for the underscore (_) and the dollar ($) sign, they cannot contain spaces or special characters.
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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name the access modifiers supported in TypeScrip

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain Loops in Typescript

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A

You can convert a string to a number by using parseInt(), parseFloat() and Number(‘’) Method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Declare Keyword in TypeScript?

A

Since JavaScript lacks a TypeScript declaration, the declare keyword is used to include it in a TypeScript file without causing a compilation issue. Ambient methods and declarations use the term to define a variable that already exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to create objects in Typescript?

A

Objects are collections of keys and values that resemble a dictionary. The keys must be one-of-a-kind. They resemble arrays and are sometimes referred to as associative arrays. An array, on the other hand, employs numbers to index the values, whereas an object lets you use any type as the key.

An Object type in TypeScript refers 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

};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain Different Data Types in Typescript?

A

Number

Number

Both Integer and Floating-Point numbers are represented by it.

Boolean

Boolean

True and false values are represented.

String

String

It’s used to denote a string of characters.

Void

Void

Usually applied to function return types.

Null

Null

It’s used when an object isn’t worth anything.

Undefined

Undefined

Indicates the value assigned to an uninitialized variable.

Any

Any

Any type of value can be assigned to a variable if it is declared with any data-type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is meant by Type Inference?

A

When you don’t specify an explicit type for a variable, TypeScript can infer it. Type inference is the term for this. This is normally done during the declaration, when the variables or parameters are initialized.

TypeScript recognises that the variable koo is a string, even if you don’t mention string as a type.

let koo = “Hello world”;

console.log(typeof koo); // “string”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain Tuples in Typescript With Example

A

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”];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Anonymous Function in TypeScript?

A

Anonymous functions are those that have no identifier (function name) attached to them. At runtime, these functions are dynamically declared. Anonymous functions, like normal functions, can accept inputs and return outputs. After its initial creation, an anonymous function is normally inaccessible. An anonymous function can be assigned to variables.

Syntax:

var res = function( [arguments] ) { … }

Example:

var msg = function() {

return “hello world”;

}

console.log(msg())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

declarations

A

The Decorator is a type of declaration that is used to decorate a class declaration, method, accessor, property, or argument. Decorators take the form @expression, where expression must evaluate to a function that will be called with information about the decorated declaration when it is called at runtime.

Example:

function color(value: string) {

// this is the decorator factory, it sets up

// the returned decorator function

return function (target) {

// this is the decorator

// do something with ‘target’ and ‘value’…

};

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

classes

A

In terms of OOPs, a class is a template for producing objects. Object-oriented programming elements like as classes, interfaces, polymorphism, and data binding are all supported by TypeScript. The term “object” refers to a physical entity. Classes were not supported in JavaScript ES5 or earlier. This is a feature that Typescript inherits from ES6.

A class is a collection of items with similar characteristics. Fields, methods, constructors, Blocks, Nested classes, and interfaces are all included in the class.

Syntax to declare a class:

class class_Name{

field;    

method;    

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are Mapped Types

A

Taking an existing type and making each of its properties optional is a typical undertaking.

Because this happens frequently enough in JavaScript, TypeScript has a feature called mapped types that allows you to define new types based on existing ones. The new type turns each property in the old type in the same way into a mapped type. You can, for example, make all properties optional or of the readonly type.

It’s important to note that this syntax refers to a type rather than a member. You can use an intersection type to add more members:

Now, take a look at the simple mapped type and its parts:

type Keys = “option1” | “option2”;

type Flags = { [K in Keys]: boolean };

The syntax is similar to that of index signatures with a for.. in the middle. There are three sections in total:

The type variable K is assigned to each property one by one.

The literal union of strings The names of the properties to iterate over are stored in keys.

The property’s type as a result.

17
Q

conditional

A

Based on a condition given as a type relationship test, a conditional type chooses one of two alternative types:

T extends U ? X : Y

When T can be assigned to U, the type is X, and when it can’t, the type is Y.

Because the condition depends on one or more type variables, a conditional type T extends U? X: Y and is either resolved to X or Y or delayed. Whether to resolve to X or Y, or to defer, when T or U contains type variables is determined by whether the type system has enough information to conclude that T is always assignable to U.

18
Q

Explain the Awaited Type and Promise Improvements

A

The Awaited type is a new utility type introduced in TypeScript 4.5. This type is intended to represent activities such as await in async functions and the.then() method on Promises - notably, the way they recursively unwrap Promises.

// A = string

type A = Awaited<Promise<string>>;</string>

// B = number

type B = Awaited<Promise<Promise<number>>>;</number>

// C = boolean | number

type C = Awaited<boolean | Promise<number>>;</number>

Existing APIs, such as JavaScript built-ins like Promise.all, Promise.race, and others, can benefit from the Awaited type. In fact, some of Promise.all’s inference concerns provided a foundation for Awaited.

Promise.all combines certain traits with Awaited to produce far superior inference results.

19
Q

What is Type Assertion? Explain its types

A

You can find yourself in a scenario where you know a type for an entity that is more specific than its present type.

A type assertion is similar to a type cast in other languages, but it does not do any additional data verification or restructuring. It has no effect on runtime and is only used by the compiler. TypeScript expects that you, the programmer, have completed any necessary specific checks.

There are two types of type assertions.

One is the as-syntax:

let someValue: unknown = “this is a string”;

let strLength: number = (someValue as string).length;

The other version is the “angle-bracket” syntax:

let someValue: unknown = “this is a string”;

let strLength: number = (<string>someValue).length;</string>

Both samples are identical. selecting one over the other is basically a matter of preference; however, only as-style assertions are allowed when combining TypeScript with JSX.