]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test: make them work
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 5 May 2022 12:31:23 +0000 (14:31 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Thu, 30 Jun 2022 07:59:00 +0000 (09:59 +0200)
__tests__/matcher/resolve.spec.ts
src/index.ts
src/router.ts
src/typedRouter.ts
test-dts/index.d.ts
test-dts/namedRoutes.test-d.ts

index 45362fc8064c13adf91703f37bc5e09ae49c5c8f..3e13f666adc39e1ad80cc2c0a0e28fc69853e57f 100644 (file)
@@ -42,7 +42,7 @@ describe('RouterMatcher.resolve', () => {
       throw new Error('not handled')
     } else {
       // use one single record
-      // @ts-expect-error: One way or the other it failse
+      // @ts-expect-error: One way or the other it false
       if (!resolved.matched) resolved.matched = record.map(normalizeRouteRecord)
       // allow passing an expect.any(Array)
       else if (Array.isArray(resolved.matched))
index a972d3268bacef5cea7d1d7ba1b52a62972d8fdb..9ee95adfe5844749b42717ba730243fcd1f36e88 100644 (file)
@@ -67,6 +67,7 @@ export type {
   _ExtractFirstParamName,
   _RemoveRegexpFromParam,
   _RemoveUntilClosingPar,
+  JoinPath,
 } from './types/paths'
 export type { RouteNamedMap } from './types/named'
 export type { Config, RouterTyped } from './typedRouter'
index faed396c1dc93b51c124aef556c500c44e93eb26..dbe6b308cf56b8caab3dbcb7fbed0f2975b6dcf1 100644 (file)
@@ -14,6 +14,7 @@ import {
   MatcherLocationRaw,
   RouteParams,
   RouteLocationNamedRaw,
+  RouteLocationPathRaw,
 } from './types'
 import { RouterHistory, HistoryState, NavigationType } from './history/common'
 import {
@@ -260,7 +261,7 @@ export interface Router<Options extends RouterOptions = RouterOptions> {
   >(
     to: RouteNamedMapGeneric extends RouteMap
       ? RouteLocationRaw
-      : RouteLocationNamedRaw<RouteMap, Name>
+      : RouteLocationNamedRaw<RouteMap, Name> | RouteLocationPathRaw | string
   ): Promise<NavigationFailure | void | undefined>
 
   /**
@@ -275,7 +276,7 @@ export interface Router<Options extends RouterOptions = RouterOptions> {
   >(
     to: RouteNamedMapGeneric extends RouteMap
       ? RouteLocationRaw
-      : RouteLocationNamedRaw<RouteMap, Name>
+      : RouteLocationNamedRaw<RouteMap, Name> | RouteLocationPathRaw | string
   ): Promise<NavigationFailure | void | undefined>
 
   /**
@@ -1185,9 +1186,7 @@ export function createRouter<Options extends RouterOptions>(
     resolve,
     options,
 
-    // @ts-expect-error: FIXME: can't type this one correctly without too much hussle
     push,
-    // @ts-expect-error: same
     replace,
     go,
     back: () => go(-1),
index 7f721bea88735fc5e0ccdbafcc974ac4ae420044..d5cf4bc3ad3cb2b7b96e73365a389fa9edee44b3 100644 (file)
@@ -1,5 +1,26 @@
-import { Router } from './router'
+import type { Router } from './router'
 
+/**
+ * Vue Router Configuration that allows to add global types for better type support.
+ *
+ * @example
+ *
+ * ```ts
+ * const router = createRouter({
+ *   // ...
+ *   routes: [
+ *     // ...
+ *   ] as const // IMPORTANT
+ * })
+ *
+ * declare module 'vue-router' {
+ *   export interface Config {
+ *     // allow global functions to get a typed router
+ *     Router: typeof router
+ *   }
+ * }
+ * ```
+ */
 export interface Config {
   // Router: unknown
 }
index 573a9fdb572c45f6f37f5f5445cc508b624178ea..1008e4d73c066ba903de6c42fb99f9e9327aa92e 100644 (file)
@@ -1,5 +1,5 @@
-// export * from '../dist/vue-router'
-export * from '../src'
+export * from '../dist/vue-router'
+// export * from '../src'
 
 export function describe(_name: string, _fn: () => void): void
 export function expectType<T>(value: T): void
index 9b0d5953df39ac12c28634a2cf7f0f15861715d8..128b2c8df4ced03d194b6fe2866bf9fbc29a5356 100644 (file)
@@ -5,11 +5,10 @@ import {
   expectType,
   RouteNamedMap,
   RouterTyped,
-  Router,
   RouteLocationRaw,
+  JoinPath,
 } from './index'
 import { DefineComponent } from 'vue'
-import { JoinPath } from 'src/types/paths'
 
 declare const component: DefineComponent
 declare const components: { default: DefineComponent }
@@ -59,7 +58,7 @@ for (const method of methods) {
   r2[method]({ name: 'UserDetails', params: { id: '2' } })
   // accepts numbers
   r2[method]({ name: 'UserDetails', params: { id: 2 } })
-  // @ts-expect-error: fails an null
+  // @ts-expect-error: fails on null
   r2[method]({ name: 'UserDetails', params: { id: null } })
   // @ts-expect-error: and undefined
   r2[method]({ name: 'UserDetails', params: { id: undefined } })
@@ -77,10 +76,15 @@ for (const method of methods) {
   r2[method]({ name: Symbol() })
   // any path is still valid
   r2[method]('/path')
+  r2.push('/path')
+  r2.replace('/path')
   // relative push can have any of the params
   r2[method]({ params: { a: 2 } })
   r2[method]({ params: {} })
   r2[method]({ params: { opt: 'hey' } })
+  // FIXME: is it possible to support this version
+  // // @ts-expect-error: does not accept any params
+  // r2[method]({ name: 'nested', params: { eo: 'true' } })
 }
 
 r2.push({} as unknown as RouteLocationRaw)
@@ -133,8 +137,8 @@ expectType<{
   // @ts-expect-error
 }>(createMap(r2.options.routes))
 
-declare module '../src' {
-  // declare module '../dist/vue-router' {
+// declare module '../src' {
+declare module '../dist/vue-router' {
   export interface Config {
     Router: typeof r2
   }