TypeScript Flashcards

Cover different topics in TypeScript.

1
Q

ECMA Script 2015 introduced which two new keywords for declaring variables, and constants ?

A

let , const

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

What is the main difference between declaring a variable using keyword “var” or using keywords “let” or “const” ?

A

The main difference is in the scoping rules applied to the identifiers declared with them. Variables using “var” are globally available within the function they were declared in, while variables declared with “let” and “const” are local to the block they were declared in.

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

How can one identify a “block” ?

A

Usually a block is defined by a set of curly braces.

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

What is another difference between variables declared with “var” and variables declared with “let” and “const” ?

A

Variables declared with “var” are hoisted to the top of the function they were declared in, meaning, the run time behavior will be as if it was declared on the top even if it was declared on the bottom of a function. Variables declared with “let” and “const” are only available where they were declared.

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

What is the main difference between declaring a variable using keyword “var” or using keywords “let” or “const” ?

A

When using “var” , it is possible to declare the same variable name twice in the same function. This is usually not good practice, and with “let” and “const” you can only declare one name per block, avoiding this problem all-together.

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

What are the different “types” used in TypeScript ?

A

Boolean, Number, String, Array, Tuple, Enum, Any, Void, Null, Undefined, and Never.

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

What is a boolean’s value ?

A

True or false .

let isDone: boolean = false;

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

What is a number’s value ?

A

Any integer. It is not treated like a string !

let decimal: number = 6;
let hex: number = 0xf00d;
let binary: number = 0b1010;
let octal: number = 0o744;

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

What is a string’s value ?

A

Anything between “ “ .

let color: string = “blue”;
color = ‘red’;

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

What is an array’s value ?

A

A collection of more then 1 of a certain type .

let list: number[ ] = [1, 2, 3];

–this is a more generic way–
let list: Array = [1, 2, 3];

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

What is a Tuple’s value ?

A

Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same. For example, you may want to represent a value as a pair of a string and a number:

// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Enum’s value ?

A

An enum is a way of giving more friendly names to sets of numeric values.

enum Color {Red, Green, Blue};
let c: Color = Color.Green;

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

Enums begin numbering their members starting at what ?

A

Enums start numbering at 0, but you can change that by manually setting the value of one of its members, or all of its members.

enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green;

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

What is a handy feature of Enums ?

A

You can also go from a numeric value to the name of that value in the enum. For example, if we had the value 2 but weren’t sure what that mapped to in the Color enum we could look up the corresponding name:

enum Color {Red = 1, Green, Blue};
let colorName: string = Color[2];

alert(colorName);

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

What is Any’s value ?

A

It’s any value ! Allowing you to gradually opt-in and opt-out of type-checking during compilation.

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

What is Void’s value ?

A

the opposite of any: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value:

function warnUser(): void {
    alert("This is my warning message");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is Null and Undefined ?

A

By default null and undefined are subtypes of all other types. That means you can assign null and undefined to something like number.

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

What is Never’s value ?

A

The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns.

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

How do you assign a type to a variable ?

A

: type // after the variable

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

What happens if you don’t assign a type ?

A

TypeScript will infer one based on how you used the variable.

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

How do you assign a type to the return value of a function ?

A

: type // after the ) of the function parameters.

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

Find the problem :

function sumMatrix(matrix: number[ ][ ]) {
    var sum = 0;
    for (var i = 0; i < matrix.length; i++) {
        var currentRow = matrix[i];
        for (var i = 0; i < currentRow.length; i++) {
            sum += currentRow[i];
        }
    }
return sum; }
A

The inner for-loop will accidentally overwrite the variable i because i refers to the same function-scoped variable.

23
Q

What is the output of the following ? Why ?

for (var i = 0; i < 10; i++) {
    setTimeout(function() { console.log(i); }, 100 * i);
}
A

The output will be 10, 10, 10 etc. The reason : setTimeout will run a function after some number of milliseconds(though waiting for anything else to stop running), but only after the for loop has stopped executing; By the time the for loop has stopped executing, the value of i is 10. So each time the given function gets called, it will print out 10!

24
Q

How do you avoid a 10, 10, 10 with setTimeout ?

A

Immediately Invoked Function Expression:

for (var i = 0; i < 10; i++) {
    // capture the current state of 'i'
    // by invoking a function with its current value
    (function(i) {
        setTimeout(function() { console.log(i); }, 100 * i);
    })(i);
}
25
Q

How are “let” statements written ?

A

let hello = “Hello!”;

26
Q

What is lexical-scoping or better know as block-scoping ?

A

Unlike variables declared with var whose scopes leak out to their containing function, block-scoped variables are not visible outside of their nearest containing block or for-loop.

27
Q

What is the “temporal dead zone” ?

A

This is just a sophisticated way of saying you can’t access the variable before the let statement, and luckily TypeScript will let you know that.

28
Q

What is “shadowing” ?

A

The act of introducing a new name in a more nested scope.

29
Q

What is the output of the following? Why ?

for (let i = 0; i < 10 ; i++) {
    setTimeout(function() { console.log(i); }, 100 * i);
}
A

0, 1, 2, 3.. etc The reason : “let” declarations have drastically different behavior when declared as part of a loop. Rather than just introducing a new environment to the loop itself, these declarations sort of create a new scope per iteration.

30
Q

What is a “const” ?

A

Its a way to declare an variable, they are like “let” declarations but, as their name implies, their value cannot be changed once they are bound. In other words, they have the same scoping rules as “let”, but you can’t re-assign to them.

31
Q

Are const’s immutable ?

A

NO ! Unless you take specific measures to avoid it, the internal state of a const variable is still modifiable. Fortunately, TypeScript allows you to specify that members of an object are readonly.

32
Q

When should you use “const” ?

A

All declarations other than those you plan to modify should use “const”.

33
Q

When should you use “let” ?

A

All declarations you plan to modify should use “let”.

34
Q

What does … do ?

A

You can create a variable for the remaining items in a list using the syntax …
let [first, …rest] = [1, 2, 3, 4];
console.log(first); // outputs 1
console.log(rest); // outputs [ 2, 3, 4 ]

35
Q

What is Default value ?

A

Default values let you specify a default value in case a property is undefined:

function keepWholeObject(wholeObject: { a: string, b?: number }) {
    let { a, b = 1001 } = wholeObject;
}
36
Q

How do you make a parameter optional ?

A

you add a ? after the parameter .

37
Q

What is destructuring ?

A

Think of destructuring as an inverse of structuring. You can take a variable with properties, and create variables out of them. Array destructuring can allow you to use arrays as though they were tuples.

38
Q

What types of destructuring can you do in typescript ?

A

Array and object .

39
Q

What is an example of object deconstruction ?

A

var rect = { x: 0, y: 10, width: 15, height: 20 };

// Destructuring assignment
var {x, y, width, height} = rect;
console.log(x, y, width, height); // 0,10,15,20
40
Q

What is another example of object deconstruction ?

A
var foo = { bar: { bas: 123 } };
var {bar: {bas}} = foo; // Effectively `var bas = foo.bar.bas;`
41
Q

What is an example of array deconstruction ?

A

var x = 1, y = 2;
[x, y] = [y, x];
console.log(x, y); // 2,1

42
Q

What is an example of Array Destructuring with rest ?

A

var [x, y, …remaining] = [1, 2, 3, 4];

console.log(x, y, remaining); // 1, 2, [3,4]

43
Q

What is an example of Array Destructuring with ignores ?

A

You can ignore any index by simply leaving its location empty i.e. , , in the left hand side.
var [x, , …remaining] = [1, 2, 3, 4];
console.log(x, remaining); // 1, [3,4]

44
Q

What is an example of property renaming ?

A
let { a: newName1, b: newName2 } = o;
------the same as writing : -------
let newName1 = o.a;
let newName2 = o.b;
45
Q

How do you specify a type while destructuring ?

A

let { a, b }: { a: string, b: number } = o;

46
Q

What is the “spread” operator ?

A

The spread operator is the opposite of destructuring. It allows you to spread an array into another array, or an object into another object.

47
Q

What is an example of using a “spread” operator with arrays ?

A

let first = [1, 2];
let second = [3, 4];
let bothPlus = [0, …first, …second, 5];
This gives bothPlus the value [0, 1, 2, 3, 4, 5]. Spreading creates a shallow copy of first and second. They are not changed by the spread.

48
Q

What is an example of using a “spread” operator with objects ?

A
let defaults = { food: "spicy", price: "\$\$", ambiance: "noisy" };
let search = { ...defaults, food: "rich" };
49
Q

How is “spread” different in objects then arrays ?

A

Like array spreading, it proceeds from left-to-right, but the result is still an object. This means that properties that come later in the spread object overwrite properties that come earlier.

50
Q

What some limits of “object spread” ?

A

First, it only includes own, enumerable properties. Basically, that means you lose methods when you spread instances of an object. Second, the Typescript compiler doesn’t allow spreads of type parameters from generic functions. That feature is expected in future versions of the language.

51
Q

What is “duck typing” ? If it looks like a duck, walks like a duck, and quacks like a duck…its a DUCK !

A

One of TypeScript’s core principles is that type-checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural subtyping”. In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.

52
Q

What is an example of a simple interface?

A
function printLabel(labelledObj: { label: string }) {
    console.log(labelledObj.label);
}
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
53
Q
What is a "readonly property" ?
interface Point {
    readonly x: number;
    readonly y: number;
}
A

Some properties should only be modifiable when an object is first created. You can specify this by putting readonly before the name of the property.