Skip to content

Commit

Permalink
docs: describe sync/async atoms creation in TypeScript (pmndrs#1592)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrzej Fricze <[email protected]>
  • Loading branch information
fricze and fricze authored Nov 24, 2022
1 parent bfd0af0 commit 66af79b
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/guides/async.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ const Component = () => {
}
```

### Usage in TypeScript

In TypeScript `atom(0)` is inferred as `PrimitiveAtom<number>`. It cannot accept `Promise<number>` as a value so preceding code would not typecheck. To accomodate for that you need to type your atom explicitly and add `Promise<number>` as accepted value.

```ts
const baseAtom = atom<number | Promise<number>>(0) // Will accept sync and async values
```

## Async forever

Sometimes you may want to suspend until an unpredetermined moment (or never).
Expand Down

0 comments on commit 66af79b

Please sign in to comment.