Skip to content

Commit

Permalink
Maybe and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JAForbes committed Feb 13, 2024
1 parent 09add29 commit 603b57c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lib/index.ts
Expand Up @@ -263,8 +263,13 @@ export function either<Name extends string, Yes, No>(
return api as EitherApi<Name, Yes, No>
}

// alias
export { either as maybe }

export function maybe<Name extends string, Yes>(
name: Name,
yes: (_: Yes) => any
) {
return either(name, yes) as any as EitherApi<Name, Yes, Record<string, never>>
}

export function Resource<Name extends string, Value extends any>(name: Name) {
const Resource = type(name, {
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Expand Up @@ -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.
Expand Down Expand Up @@ -675,4 +675,10 @@ const ExampleInternal = T.type('Example', {
const myFunction = (a: T.Instance<typeof ExampleInternal>) => { ... }

export default { ...ExampleInternal, myFunction }
```
```
### 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<string, never>` 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.

0 comments on commit 603b57c

Please sign in to comment.