From: Eduardo San Martin Morote Date: Thu, 27 Jan 2022 10:50:32 +0000 (+0100) Subject: fix(types): custom Awaited for TS 4.x X-Git-Tag: pinia@2.0.10~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7c51126d5b59b0c1a693df7c4a93bd4cf309b79b;p=thirdparty%2Fvuejs%2Fpinia.git fix(types): custom Awaited for TS 4.x Fix #957 --- diff --git a/packages/pinia/src/types.ts b/packages/pinia/src/types.ts index 5b2d3dad..20a0c532 100644 --- a/packages/pinia/src/types.ts +++ b/packages/pinia/src/types.ts @@ -159,6 +159,16 @@ export type SubscriptionCallback = ( state: UnwrapRef ) => void +// to support TS 4.4 +// TODO: remove in 2.1.0, use Awaited, and up the peer dep to TS 4.5 +type _Awaited = 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 // recursively unwrap the value + : never // the argument to `then` was not callable + : T // non-object or non-thenable + /** * Actual type for {@link StoreOnActionListenerContext}. Exists for refactoring * purposes. For internal use only. @@ -194,12 +204,12 @@ export interface _StoreOnActionListenerContext< after: ( callback: A extends Record ? ( - resolvedReturn: Awaited> + resolvedReturn: _Awaited> // allow the after callback to override the return value ) => | void | ReturnType - | Awaited> + | _Awaited> : () => void ) => void