typescript-monads helps you write safer code by using abstractions over messy control flow and state.
You can use this library in the browser, node, or a bundler
npm install typescript-monads
<head>
<script src="https://unpkg.com/typescript-monads"></script>
<!-- or use a specific version to avoid a redirect -->
<script src="https://unpkg.com/[email protected]/index.min.js"></script>
</head>
var someRemoteValue;
typescriptMonads.maybe(someRemoteValue).tapSome(console.log)
import { maybe } from 'typescript-monads'
// safely map values
let maybeVisitedBeforeXTimes: number | undefined = 50
const priceWithDiscountForLoyalty = maybe(maybeVisitedBeforeXTimes)
.match({
some: visits => 15.00 - visits * 0.1,
none: () => 15.00
})
// handle multiple maybe conditionas together
const canRideCoaster = getAge() // Maybe<number>
.bind(age => getTicket(age)) // Maybe<Ticket>
.match({
some: ticket => ticket.canRide('coaster1'),
none: () => false
})
// operations with side-effects
maybe(process.env.DB_URL)
.do({
some: dbUrl => {
// value exists, can connect
},
none: () => console.info('no url provided, could not connect to the database')
})
TODO
TODO
TODO