Typescript 1 Flashcards

1
Q

What is a statically typed language?

A

It’s type-checked and compiled before you can run it.

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

In Typescript, can you change the type of a variable partway through a program?

A

No

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

What does Typescript need to be compiled into before it’s run in a browser?

A

TypeScript needs to be compiled into JavaScript before it can be run in a browser.

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

When do you receive an error if your code is not correctly typed in Typescript?

A

At compile time.

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

What are the 2 ways you can declare a type in Typescript?

A

Explicit type annotations:
let a: number = 1 // a is a number
let b: string = ‘hello’ // b is a string
let c: boolean[] = [true, false] // c is an array of

Inferred types:
let a = 1 // a is a number
let b = ‘hello’ // b is a string
let c = [true, false] // c is an array of Booleans

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

What is the command for the Typescript compiler?

A

$> tsc hello_world.ts

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

What is the Any type?

A

The any type is a catch-all type:
let a: any = 123
let b: any = [‘danger’]
let c = a + b // Value is ‘123danger’

Use any sparingly. Overuse can remove the benefits of type-checking

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

What is the Unknown type?

A

The unknown type is a safer counterpart to any.

TypeScript will never infer something as unknown. You have to explicitly annotate it:
let a: unknown = 30;

It only supports a limited set of operations. You can just compare
unknown values (using ==, ===, !=, !==)

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

What is a Literal type?

A

Literal types allow you to specify the exact value a variable must have.

let e: true = true, e can only ever be true.

let f: 26.218 = 26.218

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

What is a symbol type?

A

TypeScript has a symbol type which is unique and immutable data type and is often used to identify object properties.

let a = Symbol('a') // symbol

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

How do you define an object in Typescript?

A

let a: object = {
b: ‘x’
}
console.log(a[‘b’])

let c: { d : string } = {
d: ‘y’
}
console.log(c.d)
The type {d: string} means an object with a string property d

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

How are object types enforced in Typescript?

A

TypeScript enforces the object shape defined by the type.

let a: {b: number}

a = {} // Error TS2741: Property ‘b’ is missing in type ‘{}’
// but required in type ‘{b: number}’.

a = {
b: 1,
c: 2 // Error TS2322: Type ‘{b: number; c: number}’ is not
} // assignable to type ‘{b: number}’

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

How to define an optional value in Typescript?

A

c? means c is an optional property.

let a: {
b: number
c?: string
[key: number]: boolean
}

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

How to define a read only property in Typescript?

A

TypeScript allows properties to be marked as ‘readonly‘ which means
they can’t be reassigned after initialisation.

let user: {
readonly firstName: string
} = {
firstName: ‘abby’
}

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

How to declare Type Aliases in Typescript?

A

You can create new names for types using type aliases:

type Age = number

type Person = {
name: string
age: Age
}

let age: Age = 55

let driver: Person = {
name: ‘James May’,
age: age
}

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

How do you use a union operator in Typescript?

A

type CatOrDog = Cat | Dog

Cat | Dog creates a new type that can be either Cat or Dog (or both)

17
Q

How do you use an intersection operator in Typescript?

A

type CatAndDog = Cat & Dog

Cat & Dog creates a new type that combines Cat and Dog.

18
Q

How do you declare a tuple in Typescript?

A

// A tuple of [first name, last name, birth year]

let b : [string, string, number]
= [‘Malcolm’, ‘Gladwell’, 1963]

19
Q

Can you have optionals in a tuple in Typescript?

A

You can specify optional elements in a tuple type using the ? operator.

// An array of train fares, which sometimes vary depending on direction

let trainFares: [number, number?][] = [
[3.75],
[8.25, 7.70],
[10.50]
]

20
Q

What is a rest element in a tuple in Typescript?

A

TypeScript allows you to specify rest elements in a tuple type using the … operator.

// A list of strings with at least 1 element
let friends: [string, …string[]]
= [‘Sara’, ‘Tali’, ‘Chloe’, ‘Claire’]

In friends, the rest elements are of type string[].
This means friends must have at least one element, and the rest of the elements (if any) must be strings.

21
Q

What do apply call and bind do in Typescript?

A

TypeScript supports the apply, call, and bind methods on functions:

function add(a: number, b: number): number {
return a + b
}

add(10, 20) // evaluates to 30
add.apply(null, [10, 20]) // evaluates to 30
add.call(null, 10, 20) // evaluates to 30
add.bind(null, 10, 20)() // evaluates to 30

Apply and call invoke the function whereas bind modifies the function

22
Q

How do you create a generic type in Typescript?

A

TypeScript allows you to create generic types.

type Filter = {
<T>(array: T[], f: (item: T) => boolean): T[]
}

let myFilter: Filter = (array, f) => array.filter(f)

Here, T is a type parameter. It’s a placeholder for the actual type that will be passed to the Filter type.

23
Q

What is the difference between these filters?

type Filter1 = {
<T>(array: T[], f: (item: T) => boolean): T[]
}
let filter1: Filter1 = (array, f) => [ /*...*/]

type Filter2<T> = {
(array: T[], f: (item: T) => boolean): T[]
}
let filter2: Filter2<number> = (array, f) => [ /*...*/]

A

Notice the difference between the definitions of Filter1 and Filter2:
In Filter1, the <T> is on the right of the assignment operator
In Filter2, the <T> is on the left of the assignment operator</T></T>

This difference has the consequence that the type T is bound at different
times for Filter1 and Filter2:

Filter1 usage:
Type T is instantiated at call time, based on the arguments passed to filter1:
let result1 = filter1([1, 2, 3], x => x > 2);
let result2 = filter1([“a”, “b”, “c”], x => x !== “b”);

Filter2 usage:
Type T is fixed when Filter2 is defined (i.e. filter2: Filter2<number>):
let result3 = filter2([1, 2, 3], x => x > 2);
let result4 = filter1(["a", "b", "c"], x => x !== "b"); //Error</number>