From 3473d1fb6ad29727570ee705a5071001a4fd556f Mon Sep 17 00:00:00 2001 From: Hung Pham Date: Sun, 3 May 2026 12:48:31 +0700 Subject: [PATCH] feat(challenge 15): update createVehicle function overloads and improve error handling --- .../src/app/app.component.ts | 4 +- .../src/app/vehicle.utils.ts | 60 ++++++++++++++++--- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/apps/typescript/15-function-overload/src/app/app.component.ts b/apps/typescript/15-function-overload/src/app/app.component.ts index 1f06369c1..d8843095b 100644 --- a/apps/typescript/15-function-overload/src/app/app.component.ts +++ b/apps/typescript/15-function-overload/src/app/app.component.ts @@ -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'); } diff --git a/apps/typescript/15-function-overload/src/app/vehicle.utils.ts b/apps/typescript/15-function-overload/src/app/vehicle.utils.ts index bec95c08d..e0391e411 100644 --- a/apps/typescript/15-function-overload/src/app/vehicle.utils.ts +++ b/apps/typescript/15-function-overload/src/app/vehicle.utils.ts @@ -28,10 +28,18 @@ 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) { @@ -39,17 +47,51 @@ export function createVehicle( 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 }; +// } +// }