Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/fc0c379ed5f81b3b74ede56499a45b79 to your computer and use it in GitHub Desktop.
Save davidystephenson/fc0c379ed5f81b3b74ede56499a45b79 to your computer and use it in GitHub Desktop.
type Input = string[] | number[]
interface Tree {
branches: number
species: string
height: number
rings: number
}
interface WildTree extends Tree {
ecosystem: string
}
const wild: WildTree = {
ecosystem: 'jungle',
branches: 10,
species: 'palm',
rings: 5,
height: 20
}
type TreeKey = keyof Tree
const oak: Tree = {
branches: 10,
species: 'oak',
height: 50,
rings: 20
}
const willow: Tree = {
branches: 100,
species: 'willow',
height: 200,
rings: 70
}
function describeTree (tree: Tree, key: TreeKey) {
const value = tree[key]
const message = `The ${key} of this tree is ${value}`
console.log(message)
}
describeTree(oak, 'species')
describeTree(willow, 'branches')
describeTree(willow, 'height')
describeTree(oak, 'rings')
interface Vehicle {
speed: number
seats: number
}
class Car implements Vehicle {
private position: number
public speed: number
public maker: string
public seats: number
constructor (speed: number, maker: string, seats: number) {
this.position = 0
this.speed = speed
this.maker = maker
this.seats = seats
}
travel () {
this.position += this.speed
}
}
class Racecar extends Car {
team: string
constructor(speed: number, maker: string, team: string, seats: number) {
super(speed, maker, seats)
this.team = team
}
}
const myCar = new Car(80, 'scion', 3)
console.log(myCar.speed)
myCar.travel()
function describeVehicle (vehicle: Vehicle) {
console.log('The speed is:', vehicle.speed)
}
describeVehicle(myCar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment