From: Eduardo San Martin Morote Date: Thu, 5 May 2022 12:31:23 +0000 (+0200) Subject: test: make them work X-Git-Tag: v4.1.0~105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3de8be375dfa2d8c6104c6344c75758a25ae79bc;p=thirdparty%2Fvuejs%2Frouter.git test: make them work --- diff --git a/__tests__/matcher/resolve.spec.ts b/__tests__/matcher/resolve.spec.ts index 45362fc8..3e13f666 100644 --- a/__tests__/matcher/resolve.spec.ts +++ b/__tests__/matcher/resolve.spec.ts @@ -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)) diff --git a/src/index.ts b/src/index.ts index a972d326..9ee95adf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/router.ts b/src/router.ts index faed396c..dbe6b308 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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 { >( to: RouteNamedMapGeneric extends RouteMap ? RouteLocationRaw - : RouteLocationNamedRaw + : RouteLocationNamedRaw | RouteLocationPathRaw | string ): Promise /** @@ -275,7 +276,7 @@ export interface Router { >( to: RouteNamedMapGeneric extends RouteMap ? RouteLocationRaw - : RouteLocationNamedRaw + : RouteLocationNamedRaw | RouteLocationPathRaw | string ): Promise /** @@ -1185,9 +1186,7 @@ export function createRouter( 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), diff --git a/src/typedRouter.ts b/src/typedRouter.ts index 7f721bea..d5cf4bc3 100644 --- a/src/typedRouter.ts +++ b/src/typedRouter.ts @@ -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 } diff --git a/test-dts/index.d.ts b/test-dts/index.d.ts index 573a9fdb..1008e4d7 100644 --- a/test-dts/index.d.ts +++ b/test-dts/index.d.ts @@ -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(value: T): void diff --git a/test-dts/namedRoutes.test-d.ts b/test-dts/namedRoutes.test-d.ts index 9b0d5953..128b2c8d 100644 --- a/test-dts/namedRoutes.test-d.ts +++ b/test-dts/namedRoutes.test-d.ts @@ -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 }