Skip to content

Answer:15 #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
9 changes: 6 additions & 3 deletions apps/typescript/15-function-overload/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { createVehicle } from './vehicle.utils';
template: ``,
})
export class AppComponent {
bicycle = createVehicle('bicycle');

car = createVehicle('car', 'diesel');
moto = createVehicle('moto', 'diesel');
bus = createVehicle('bus', undefined, 20);
boat = createVehicle('boat', undefined, 300, true);
bicycle = createVehicle('bicycle');

boat = createVehicle('boat', 300);

bus = createVehicle('bus', 20, true);
}
53 changes: 41 additions & 12 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,57 @@ interface Boat {

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

export function createVehicle(type: 'bicycle'): Bicycle;
export function createVehicle(type: 'car', fuel: Fuel): Car;
export function createVehicle(type: 'moto', fuel: Fuel): Moto;
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 (!isFuel(fuelOrCapacity)) {
throw new Error(`'fuel' must be provided for type "${type}".`);
}
return { fuel: fuelOrCapacity, type };

case 'boat':
if (!capacity)
throw new Error(`capacity property is missing for type boat`);
return { capacity, type };
if (typeof fuelOrCapacity !== 'number') {
throw new Error(`'capacity' must be a number for type "boat".`);
}
return { capacity: fuelOrCapacity, 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 };
if (typeof fuelOrCapacity !== 'number') {
throw new Error(`'capacity' must be a number for type "bus".`);
}
if (typeof isPublicTransport !== 'boolean') {
throw new Error(
`'isPublicTransport' must be a boolean for type "bus".`,
);
}
return {
capacity: fuelOrCapacity,
isPublicTransport,
type,
};
}
}

function isFuel(value: unknown): value is Fuel {
return (
typeof value === 'string' &&
['diesel', 'petrol', 'electric'].includes(value)
);
}