diff --git a/lib/index.ts b/lib/index.ts index 693e8d9..0deea1c 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -263,8 +263,13 @@ export function either( return api as EitherApi } -// alias -export { either as maybe } + +export function maybe( + name: Name, + yes: (_: Yes) => any +) { + return either(name, yes) as any as EitherApi> +} export function Resource(name: Name) { const Resource = type(name, { diff --git a/readme.md b/readme.md index 3589982..2c3d171 100644 --- a/readme.md +++ b/readme.md @@ -347,9 +347,9 @@ Resource.getLoaded(instance, defaultValue, getter) |---|---|---|---| | `Resource.Loaded({ id: 'hello', title: 'cool' })` | `-` | `-` | `{ id: 'hello', title: 'cool' }` | | `Resource.Loading(55)` | `-` | `-` | `null` | -| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello` | `-` | `null` | +| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello'` | `-` | `null` | | `Resource.Loading(55)` | `'hello'` | `-` | `'hello'` | -| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello` | `x => x.title` | `'cool'` | +| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello'` | `x => x.title` | `'cool'` | | `Resource.Loading(55)` | `'hello'` | `x => x.title` | `'hello'` | If you just want to get the `instance` `value` if it is the given `tag` do not supply `defaultValue` or `getter`. It will return `null` if the instance is any other case. @@ -675,4 +675,10 @@ const ExampleInternal = T.type('Example', { const myFunction = (a: T.Instance) => { ... } export default { ...ExampleInternal, myFunction } -``` \ No newline at end of file +``` + +### Why can't I call a constructor with no arguments? + +80% of this library is type definitions. We could add the ability for some function signatures to be parameterless, but it just adds to the complexity of the library for a tiny syntax cost. + +We tend to use `Record` for these cases to enforce an empty object is passed in. But you could use `null`, or `undefined`. But you need to pass exactly 1 argument into your constructor. \ No newline at end of file