Skip to content

Commit 7c51126

Browse files
committed
fix(types): custom Awaited for TS 4.x
Fix #957
1 parent aea1c3f commit 7c51126

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/pinia/src/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ export type SubscriptionCallback<S> = (
159159
state: UnwrapRef<S>
160160
) => void
161161

162+
// to support TS 4.4
163+
// TODO: remove in 2.1.0, use Awaited, and up the peer dep to TS 4.5
164+
type _Awaited<T> = T extends null | undefined
165+
? T // special case for `null | undefined` when not in `--strictNullChecks` mode
166+
: T extends object & { then(onfulfilled: infer F): any } // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
167+
? F extends (value: infer V, ...args: any) => any // if the argument to `then` is callable, extracts the first argument
168+
? Awaited<V> // recursively unwrap the value
169+
: never // the argument to `then` was not callable
170+
: T // non-object or non-thenable
171+
162172
/**
163173
* Actual type for {@link StoreOnActionListenerContext}. Exists for refactoring
164174
* purposes. For internal use only.
@@ -194,12 +204,12 @@ export interface _StoreOnActionListenerContext<
194204
after: (
195205
callback: A extends Record<ActionName, _Method>
196206
? (
197-
resolvedReturn: Awaited<ReturnType<A[ActionName]>>
207+
resolvedReturn: _Awaited<ReturnType<A[ActionName]>>
198208
// allow the after callback to override the return value
199209
) =>
200210
| void
201211
| ReturnType<A[ActionName]>
202-
| Awaited<ReturnType<A[ActionName]>>
212+
| _Awaited<ReturnType<A[ActionName]>>
203213
: () => void
204214
) => void
205215

0 commit comments

Comments
 (0)