type Vehicle = {
color: string;
};
function drive(v: Vehicle, distance: number) {
console.log(`driving ${distance} units in a ${v.color} vehicle`);
};
const vehicle: Vehicle = {
color: "purple",
};
drive(vehicle, 12);
const obj = {
color: "blue",
};
drive(obj, 10);
const obj2 = {
color: "green",
hasLeatherSeats: true,
};
drive(obj2, 10);
type Truck = {
color: string;
carryingCapacity: number;
};
function carry(t: Truck, load: number) {
if (load > t.carryingCapacity) {
console.log(`failed to carry ${load} units in a ${t.color} truck`);
} else {
console.log(`carrying ${load} units in a ${t.color} truck`);
}
};
const truck: Truck = {
color: "red",
carryingCapacity: 8,
};
carry(truck, 7);