Typescript 2 Flashcards

1
Q

How do you declare that a class inherits from its superclass in Typescript?

A

class King extends Piece {}
class Queen extends Piece {}
class Bishop extends Piece {}

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

In Typescript how do you declare a constructor for a class?

A

type Color = ‘Black’ | ‘White’
type FileVal = ‘A’ | ‘B’ | ‘C’ | ‘D’ | ‘E’ | ‘F’ | ‘G’ | ‘H’
type RankVal = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8

class Position {
constructor(
private file: FileVal,
private rank: RankVal
) {}
}

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

What access do you have to a property with a private modifier in Typescript?

A

The private modifier allows access within the same class.

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

What access do you have to a property with a protected modifier in Typescript?

A

The protected modifier allows access within the same class and sub-classes.

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

What access do you have to a property with a public modifier in Typescript?

A

The public modifier allows access from any location.

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

What is an abstract class in Typescript?

A

Abstract classes cannot be instantiated directly. They are meant to be
sub-classed by concrete (non-abstract) classes, which are instantiated.

In our example, one would not instantiate a Piece directly, but rather a
King, Queen, Bishop, etc. ( where these all extend piece)

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

How do we implement an abstract class from a superclass in Typescript? E.g. canMoveTo

abstract class Piece {
// …
moveTo(position: Position) {
this.position = position
}
abstract canMoveTo(position: Position): boolean
}

A

Abstract methods from an abstract class must be implemented in a non-abstract child class:

class King extends Piece {
canMoveTo(position: Position) {
let distance = this.position.distanceFrom(position)
return distance.rank < 2 && distance.file < 2
}
}

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

When are static methods called/created in Typescript?

A

Static methods are created once for the class, rather than for each instance of the class.
They can’t access instance members, but they can access other static members.

class Game {
private pieces = Game.makePieces()
private static makePieces() {
return [ new King(‘White’, ‘E’, 1),
new King(‘Black’, ‘E’, 8),
new Queen(‘White’, ‘D’, 1),
new Queen(‘Black’, ‘D’, 8),
new Bishop(‘White’, ‘C’, 1),
new Bishop(‘White’, ‘F’, 1),
new Bishop(‘Black’, ‘C’, 8),
new Bishop(‘Black’, ‘F’, 8)]
}
}

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

What is structural typing in Typescript?

A

TypeScript uses structural typing.
This means that if two objects have the same structure, they are considered to be of the same type.

For example, let’s define two classes with the same structure:
class Zebra {
trot() {
// …
}
}
class Poodle {
trot() {
// …
}
}

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

How can we use generics in classes in Typescript?

A

class MyMap<K, V> {
constructor(initialKey: K, initialValue: V) { /* … / }
get(key: K): V { /
/ }
set(key: K, value: V): void { /
/ }
merge<K1, V1>(map: MyMap<K1, V1>): MyMap<K | K1, V | V1> {/
/}
static of<K, V>(k: K, v: V): MyMap<K, V> {/
…*/}
}

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

How do you declare an interface in Typescript?

A

Interfaces provide another way to define the types of objects. For
example, consider this object type:
type Sushi = {
calories: number
salty: boolean
tasty: boolean
}
The equivalent interface is:
interface Sushi {
calories: number
salty: boolean
tasty: boolean
}

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

Can interfaces inherit from one another?

A

interface Food {
calories: number
tasty: boolean
}
interface Sushi extends Food {
salty: boolean
}
interface Cake extends Food {
sweet: boolean
}
Sushi and Cake interfaces extend the Food interface, inheriting its properties and adding their own.

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

What happens if you have this code:

interface User {
name: string
}

interface User {
age: number
}

A

Declaration merging which is TypeScript’s way of automatically combining multiple declarations that share the same name.

User now has two fields, name and age

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

How do classes use interfaces?

A

Classes can implement interfaces.
interface Animal {
eat(food: string): void
sleep(hours: number): void
}

class Cat implements Animal {
eat(food: string) {
console.info(‘Ate some’, food, ‘. Mmm!’)
}
sleep(hours: number) {
console.info(‘Slept for’, hours, ‘hours’)
}
}

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

How many interfaces can a class inherit in Typescript?

A

Multiple.

class Cat implements Animal, Feline {
name = ‘Whiskers’
eat(food: string) {
console.info(‘Ate some’, food, ‘. Mmm!’)
}
sleep(hours: number) {
console.info(‘Slept for’, hours, ‘hours’)
}
meow() {
console.info(‘Meow’)
}
}

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