Skip to content

Commit 603b57c

Browse files
committed
Maybe and docs
1 parent 09add29 commit 603b57c

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,13 @@ export function either<Name extends string, Yes, No>(
263263
return api as EitherApi<Name, Yes, No>
264264
}
265265

266-
// alias
267-
export { either as maybe }
266+
267+
export function maybe<Name extends string, Yes>(
268+
name: Name,
269+
yes: (_: Yes) => any
270+
) {
271+
return either(name, yes) as any as EitherApi<Name, Yes, Record<string, never>>
272+
}
268273

269274
export function Resource<Name extends string, Value extends any>(name: Name) {
270275
const Resource = type(name, {

readme.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ Resource.getLoaded(instance, defaultValue, getter)
347347
|---|---|---|---|
348348
| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `-` | `-` | `{ id: 'hello', title: 'cool' }` |
349349
| `Resource.Loading(55)` | `-` | `-` | `null` |
350-
| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello` | `-` | `null` |
350+
| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello'` | `-` | `null` |
351351
| `Resource.Loading(55)` | `'hello'` | `-` | `'hello'` |
352-
| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello` | `x => x.title` | `'cool'` |
352+
| `Resource.Loaded({ id: 'hello', title: 'cool' })` | `'hello'` | `x => x.title` | `'cool'` |
353353
| `Resource.Loading(55)` | `'hello'` | `x => x.title` | `'hello'` |
354354

355355
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', {
675675
const myFunction = (a: T.Instance<typeof ExampleInternal>) => { ... }
676676

677677
export default { ...ExampleInternal, myFunction }
678-
```
678+
```
679+
680+
### Why can't I call a constructor with no arguments?
681+
682+
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.
683+
684+
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 commit comments

Comments
 (0)