Search⌘ K

Solution Review: Create a Mini DSL

Explore how to build a mini domain-specific language (DSL) in TypeScript using fp-ts. Learn to implement functions like createFlight that validate parameters and use exhaustive checks to handle arrival and departure data effectively.

We'll cover the following...

Solution

TypeScript 3.3.4
const createFlight = (typeOfFlight: KindOfFlight, date: Date) => (airplane: Airplane): O.Option<Flight> => {
if (!typeOfFlight || !date || !airplane) {
return O.none;
}
switch (typeOfFlight) {
case 'ARRIVAL': {
return O.some({
type: 'Arrival',
arrivalTime: date,
airplane,
});
}
case 'DEPARTURE': {
return O.some({
type: 'Departure',
departureTime: date,
airplane,
});
}
default:
const _exhaustiveCheck: never = typeOfFlight;
return _exhaustiveCheck;
}
};
console.log(createFlight("ARRIVAL",new Date())({seats:100}));

Explanation

  • Lines
...