Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: copy built-in awaited type #1242

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/core/atom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable

interface Getter {
<Value>(atom: Atom<Value | Promise<Value>>): Value
Expand Down
8 changes: 7 additions & 1 deletion src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
} from './suspensePromise'
import type { SuspensePromise } from './suspensePromise'

type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable

type AnyAtomValue = unknown
type AnyAtom = Atom<AnyAtomValue>
Expand Down
8 changes: 7 additions & 1 deletion src/core/useAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import type { Atom, Scope, SetAtom, WritableAtom } from './atom'
import { useAtomValue } from './useAtomValue'
import { useSetAtom } from './useSetAtom'

type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable

export function useAtom<Value, Update, Result extends void | Promise<void>>(
atom: WritableAtom<Value, Update, Result>,
Expand Down
8 changes: 7 additions & 1 deletion src/core/useAtomValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { getScopeContext } from './contexts'
import { COMMIT_ATOM, READ_ATOM, SUBSCRIBE_ATOM } from './store'
import type { VersionObject } from './store'

type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable

export function useAtomValue<Value>(
atom: Atom<Value>,
Expand Down
8 changes: 7 additions & 1 deletion src/utils/loadable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { atom } from 'jotai'
import type { Atom } from 'jotai'
import { createMemoizeAtom } from './weakCache'

type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable

const memoizeAtom = createMemoizeAtom()

Expand Down
8 changes: 7 additions & 1 deletion src/utils/selectAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { atom } from 'jotai'
import type { Atom } from 'jotai'
import { createMemoizeAtom } from './weakCache'

type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable

const memoizeAtom = createMemoizeAtom()

Expand Down
8 changes: 7 additions & 1 deletion src/utils/waitForAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { createMemoizeAtom } from './weakCache'
const memoizeAtom = createMemoizeAtom()
const emptyArrayAtom = atom(() => [])

type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: any) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable
type ResolveAtom<T> = T extends Atom<infer V> ? V : T
type AwaitedAtom<T> = Awaited<ResolveAtom<T>>

Expand Down