Skip to content

Commit

Permalink
fix: copy built-in awaited type
Browse files Browse the repository at this point in the history
  • Loading branch information
Newbie012 authored Jun 20, 2022
1 parent 535f669 commit a24684f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
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

0 comments on commit a24684f

Please sign in to comment.