This repository contains some of my more generic pieces of TypeScript code that I use over multiple projects. Some of them may be of interest to others, which is why they're open source.
git clone [email protected]:jhkuperus/ts-tools.gitnpm installnpm start
You now have a dist/ containing the build results. You can npm link in this folder to use this in your project.
You do this by finally doing npm link ts-tools in your project root folder. You are now using this package locally.
The file optional.ts contains an Optional type and several simple functions to work with it. It can be
used with any non-boolean type, but my recommendation is to use it with objects and arrays.
Here is an example of working with the types:
import { isEmpty, map, none, Optional } from 'optional'
interface User {
name: string
}
const aUser: Optional<User> = { name: "Mario" }
map(aUser, u => console.log(u.name))
const noUser: Optional<User> = none
console.log(isEmpty(map(aUser, u => console.log(u.name))))
console.log(orElse(noUser, "No user provided"))If you can't or won't import the map, flatMap or other functions because they clash with similar functions
from libraries like rxjs, you can simply use Optional.map, all functions are also available as members of
Optional