]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(types): support IDE features for store context (#924)
authorJohnson Chu <johnsoncodehk@gmail.com>
Fri, 24 Dec 2021 14:07:09 +0000 (22:07 +0800)
committerGitHub <noreply@github.com>
Fri, 24 Dec 2021 14:07:09 +0000 (15:07 +0100)
packages/pinia/src/types.ts

index a37ab76aaaa14dc9db45287cf3563ccab57f51bc..ab21cd3a889fc2a8857a53c04571a158c63fccd3 100644 (file)
@@ -543,34 +543,58 @@ export type _GettersTree<S extends StateTree> = Record<
  */
 export type _ActionsTree = Record<string, _Method>
 
+/**
+ * @internal
+ */
+type _ExtractStateFromSetupStore_Keys<SS> = keyof {
+  [K in keyof SS as SS[K] extends _Method | ComputedRef ? never : K]: any
+}
+
+/**
+ * @internal
+ */
+type _ExtractActionsFromSetupStore_Keys<SS> = keyof {
+  [K in keyof SS as SS[K] extends _Method ? K : never]: any
+}
+
+/**
+ * @internal
+ */
+type _ExtractGettersFromSetupStore_Keys<SS> = keyof {
+  [K in keyof SS as SS[K] extends ComputedRef ? K : never]: any
+}
+
+/**
+ * @internal
+ */
+type _UnwrapAll<SS> = { [K in keyof SS]: UnwrapRef<SS[K]> }
+
 /**
  * @internal
  */
 export type _ExtractStateFromSetupStore<SS> = SS extends undefined | void
   ? {}
-  : {
-      [K in keyof SS as SS[K] extends _Method | ComputedRef
-        ? never
-        : K]: UnwrapRef<SS[K]>
-    }
+  : _ExtractStateFromSetupStore_Keys<SS> extends keyof SS
+  ? _UnwrapAll<Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>>
+  : never
 
 /**
  * @internal
  */
 export type _ExtractActionsFromSetupStore<SS> = SS extends undefined | void
   ? {}
-  : {
-      [K in keyof SS as SS[K] extends _Method ? K : never]: SS[K]
-    }
+  : _ExtractActionsFromSetupStore_Keys<SS> extends keyof SS
+  ? Pick<SS, _ExtractActionsFromSetupStore_Keys<SS>>
+  : never
 
 /**
  * @internal
  */
 export type _ExtractGettersFromSetupStore<SS> = SS extends undefined | void
   ? {}
-  : {
-      [K in keyof SS as SS[K] extends ComputedRef ? K : never]: UnwrapRef<SS[K]>
-    }
+  : _ExtractGettersFromSetupStore_Keys<SS> extends keyof SS
+  ? _UnwrapAll<Pick<SS, _ExtractGettersFromSetupStore_Keys<SS>>>
+  : never
 
 /**
  * Options passed to `defineStore()` that are common between option and setup