Typescript 2 Flashcards
How do you declare that a class inherits from its superclass in Typescript?
class King extends Piece {}
class Queen extends Piece {}
class Bishop extends Piece {}
In Typescript how do you declare a constructor for a class?
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
) {}
}
What access do you have to a property with a private modifier in Typescript?
The private modifier allows access within the same class.
What access do you have to a property with a protected modifier in Typescript?
The protected modifier allows access within the same class and sub-classes.
What access do you have to a property with a public modifier in Typescript?
The public modifier allows access from any location.
What is an abstract class in Typescript?
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 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
}
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
}
}
When are static methods called/created in Typescript?
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)]
}
}
What is structural typing in Typescript?
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 can we use generics in classes in Typescript?
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 do you declare an interface in Typescript?
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
}
Can interfaces inherit from one another?
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.
What happens if you have this code:
interface User {
name: string
}
interface User {
age: number
}
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 do classes use interfaces?
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 many interfaces can a class inherit in Typescript?
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’)
}
}