Skip to content

Commit

Permalink
fix(docs): Update typescript.mdx (#1701)
Browse files Browse the repository at this point in the history
* Update typescript.mdx

Add an example about the case access to the return type

* Fixed words and the format with prettier

Fixed words and the format with prettier
  • Loading branch information
meotimdihia authored Jan 20, 2023
1 parent f798b00 commit 905b883
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions docs/guides/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Jotai relies heavily on type inferences and requires `strictNullChecks` to be en

### Primitive atoms are basically type inferred

```js
```ts
const numAtom = atom(0) // primitive number atom
const strAtom = atom('') // primitive string atom
```
Expand All @@ -43,8 +43,25 @@ const readWriteAtom = atom<string, number>(

### useAtom is typed based on atom types

```js
```ts
const [num, setNum] = useAtom(primitiveNumAtom)
const [num] = useAtom(readOnlyNumAtom)
const [, setNum] = useAtom(writeOnlyNumAtom)
```

### Access to the value type of an atom

```ts
import { ExtractAtomValue, useAtomValue } from 'jotai'
import { userAtom } from 'state'
import { useQuery } from '@tanstack/react-query'

export default function WriteReview(hid) {
const user = useAtomValue(userAtom)
const res = useGetReviewQuery(user)
}

function useGetReviewQuery(user: ExtractAtomValue<typeof userAtom>) {
return fetch('/api/user/' + user.id + '/review')
}
```

1 comment on commit 905b883

@vercel
Copy link

@vercel vercel bot commented on 905b883 Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.