Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/typescript/15-function-overload/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createVehicle } from './vehicle.utils';
export class AppComponent {
car = createVehicle('car', 'diesel');
moto = createVehicle('moto', 'diesel');
bus = createVehicle('bus', undefined, 20);
boat = createVehicle('boat', undefined, 300, true);
bus = createVehicle('bus', 20, true);
boat = createVehicle('boat', 300);
bicycle = createVehicle('bicycle');
}
60 changes: 51 additions & 9 deletions apps/typescript/15-function-overload/src/app/vehicle.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,70 @@ interface Boat {

type Vehicle = Bicycle | Car | Moto | Bus | Boat;

export function createVehicle(type: 'bicycle'): Bicycle;
export function createVehicle(type: 'moto', fuel: Fuel): Moto;
export function createVehicle(type: 'car', fuel: Fuel): Car;
export function createVehicle(type: 'boat', capacity: number): Boat;
export function createVehicle(
type: 'bus',
capacity: number,
isPublicTransport: boolean,
): Bus;
export function createVehicle(
type: VehicleType,
fuel?: Fuel,
capacity?: number,
fuelOrCapacity?: Fuel | number,
isPublicTransport?: boolean,
): Vehicle {
switch (type) {
case 'bicycle':
return { type };
case 'car':
case 'moto':
if (!fuel) throw new Error(`fuel property is missing for type ${type}`);
return { fuel, type };
if (!fuelOrCapacity || typeof fuelOrCapacity === 'number') {
throw new Error(`fuel property is missing for type ${type}`);
}

return { fuel: fuelOrCapacity, type };
case 'boat':
if (!capacity)
if (!fuelOrCapacity || typeof fuelOrCapacity === 'string') {
throw new Error(`capacity property is missing for type boat`);
return { capacity, type };
}

return { capacity: fuelOrCapacity, type };
case 'bus':
if (!capacity)
if (!fuelOrCapacity || typeof fuelOrCapacity === 'string') {
throw new Error(`capacity property is missing for type bus`);
if (!isPublicTransport)
}
if (isPublicTransport === undefined) {
throw new Error(`isPublicTransport property is missing for type bus`);
return { capacity, isPublicTransport, type };
}

return { capacity: fuelOrCapacity, isPublicTransport, type };
}
}

// export function createVehicle(
// type: VehicleType,
// fuel?: Fuel,
// capacity?: number,
// isPublicTransport?: boolean,
// ): Vehicle {
// switch (type) {
// case 'bicycle':
// return { type };
// case 'car':
// case 'moto':
// if (!fuel) throw new Error(`fuel property is missing for type ${type}`);
// return { fuel, type };
// case 'boat':
// if (!capacity)
// throw new Error(`capacity property is missing for type boat`);
// return { capacity, type };
// case 'bus':
// if (!capacity)
// throw new Error(`capacity property is missing for type bus`);
// if (!isPublicTransport)
// throw new Error(`isPublicTransport property is missing for type bus`);
// return { capacity, isPublicTransport, type };
// }
// }
Loading