Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/d42370adb6bfe6e15da002c1c9c6b0d8 to your computer and use it in GitHub Desktop.
Save davidystephenson/d42370adb6bfe6e15da002c1c9c6b0d8 to your computer and use it in GitHub Desktop.
type Input <T> = string | number | T
const input1: Input<boolean> = true
const input2: Input<string[]> = true
type Collection <T> = [T, T, T]
const stringCollection: Collection<string> = ['a', 'b', 'c']
const numberCollection: Collection<number> = ['a', 2, 3]
interface Vehicle <Cargo> {
position: number
speed: number
seats: number
wheels: number
cargo: Cargo
}
type DriverlessVehicle = Omit<Vehicle<undefined>, 'seats' | 'wheels'>
const breadVan: Vehicle<{ white: number, brown: number }> = {
position: 0,
speed: 50,
seats: 2,
wheels: 4,
cargo: { white: 10, brown: 5 }
}
const passengerVan: Vehicle<string[]> = {
position: 0,
speed: 60,
seats: 20,
wheels: 4,
cargo: ['Dorothy', 'Zelda', 'Tallulah']
}
function pilot <Cargo> (vehicle: Vehicle<Cargo>, pilotName: string): Cargo {
console.log(pilotName, 'is piloting the vehicle')
return vehicle.cargo
}
type PilotResult = ReturnType<typeof pilot>
const result = pilot(passengerVan, 'David')
const totalPassengers = 1 + result.length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment