Browse Source

types: 再次修复RouteLocationRawEx类型错误 (vbenjs#1968) (#1975)

Tanimodori 2 years ago
parent
commit
02c469b17a
1 changed files with 8 additions and 15 deletions
  1. 8 15
      src/hooks/web/usePage.ts

+ 8 - 15
src/hooks/web/usePage.ts

@@ -1,35 +1,28 @@
 import type { RouteLocationRaw, Router } from 'vue-router';
 
 import { PageEnum } from '/@/enums/pageEnum';
-import { isString } from '/@/utils/is';
 import { unref } from 'vue';
 
 import { useRouter } from 'vue-router';
 import { REDIRECT_NAME } from '/@/router/constant';
 
-export type RouteLocationRawEx = RouteLocationRaw & { path: PageEnum };
+export type PathAsPageEnum<T> = T extends { path: string } ? T & { path: PageEnum } : T;
+export type RouteLocationRawEx = PathAsPageEnum<RouteLocationRaw>;
 
 function handleError(e: Error) {
   console.error(e);
 }
 
-// page switch
+/**
+ * page switch
+ */
 export function useGo(_router?: Router) {
-  let router;
-  if (!_router) {
-    router = useRouter();
-  }
-  const { push, replace } = _router || router;
-  function go(opt: PageEnum | RouteLocationRawEx | string = PageEnum.BASE_HOME, isReplace = false) {
+  const { push, replace } = _router || useRouter();
+  function go(opt: RouteLocationRawEx = PageEnum.BASE_HOME, isReplace = false) {
     if (!opt) {
       return;
     }
-    if (isString(opt)) {
-      isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
-    } else {
-      const o = opt as RouteLocationRaw;
-      isReplace ? replace(o).catch(handleError) : push(o).catch(handleError);
-    }
+    isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
   }
   return go;
 }