TypeScript Flashcards

1
Q

Why are “Types” useful and offer an advantage compare to vanilla JavaScript?

A

Types allow you to detect errors early and avoid some runtime errors

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

Will the following code throw a compilation error?

let userName: string;
userName = ‘Maximilian’;
userName = false;

A

Yes - assigning a boolean to a variable which was assigned a “string” types is not allowed and will yield a compilation error.

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

Does this code rely on type inference?

const age: number = 29;

A

Not really - a type is assigned explicitly as well. (TypeScript would be able to infer the type (and hence you should omit “:number”) but here, we actually also have an explicit type assignment)

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

What’s the difference between JavaScript types (e.g. typeof ‘Max’ => ‘string’) and TypeScript types (e.g. const name: string = ‘…’)?

A

TS types are checked during compilation (development) JS types are checked at runtime (JS has no compilation step but at runtime, you can check for certain types (e.g. in if conditions). TS on the other hand allows you to catch certain errors during development since it checks types during compilation as well.)

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

What are the core types of TS?

A

number, string, boolean, Object, Array, Tuple, Enum, Any, Union Types, Literal Types

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

What is the Unary plus (+) operator?

A

precedes a variable and attempts to convert it to a number

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

What is an IDE?

A

IDE stands for integrated development environment (VS Code)

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

Which of the following snippets could be simplified by using an enum type?

A)
const users = [‘Max’, ‘Michael’, ‘Julie’];

B)
const userA = { name: ‘Max’ };
const userB = { name: ‘Michael’ };

C)
const ROLE_ADMIN = 0;
const ROLE_AUTHOR = 1;

A

C

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

Will the following code throw a compilation error?

type User = {name: string; age: number};
const u1: User = [‘Max’, 29];

A

YES, the “User” type clearly wants an object with a “name” and an “age” property. NOT an array.

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

Will this code make it through compilation?

type Product = {title: string; price: number;};
const p1: Product = { title: ‘A Book’, price: 12.99, isListed: true }

A

NO. isListed is not part of the “Product” type

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

Will this code make it through compilation?

type User = { name: string } | string;
let u1: User = {name: ‘Max’};
u1 = ‘Michael’;

A

This code is fine. The union type allows either an object (with a “name” property) OR a string. You can switch values how often you want.

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

Will this code compile?

function sendRequest(data: string, cb: (response: any) => void) {
// … sending a request with “data”
return cb({data: ‘Hi there!’});
}

sendRequest(‘Send this!’, (response) => {
console.log(response);
return true;
});

A

YES.
That’s correct. As you learned, callback functions can return something, even if the argument on which they are passed does NOT expect a returned value.

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

What’s the idea behind a “function type”?

A

Function types define parameters and a return type of a function

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

Which code snippet is better (i.e. which code should you write)?

1)

function sayHi(): void {
// …
}
OR

2)

function sayHi(): undefined {
// …
}

A

1 because it doesn’t force you to return anything if you don’t want to return something

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