|
| 1 | +import { describe, it, expectTypeOf } from 'vitest' |
| 2 | +import type { |
| 3 | + RouteRecordName, |
| 4 | + ParamValue, |
| 5 | + ParamValueZeroOrMore, |
| 6 | + RouteRecordInfo, |
| 7 | + RouteLocationNormalizedTypedList, |
| 8 | +} from '../src' |
| 9 | + |
| 10 | +// TODO: could we move this to an .d.ts file that is only loaded for tests? |
| 11 | +// https://github.com/microsoft/TypeScript/issues/15300 |
| 12 | +type RouteNamedMap = { |
| 13 | + home: RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>> |
| 14 | + '/[other]': RouteRecordInfo< |
| 15 | + '/[other]', |
| 16 | + '/:other', |
| 17 | + { other: ParamValue<true> }, |
| 18 | + { other: ParamValue<false> } |
| 19 | + > |
| 20 | + '/[name]': RouteRecordInfo< |
| 21 | + '/[name]', |
| 22 | + '/:name', |
| 23 | + { name: ParamValue<true> }, |
| 24 | + { name: ParamValue<false> } |
| 25 | + > |
| 26 | + '/[...path]': RouteRecordInfo< |
| 27 | + '/[...path]', |
| 28 | + '/:path(.*)', |
| 29 | + { path: ParamValue<true> }, |
| 30 | + { path: ParamValue<false> } |
| 31 | + > |
| 32 | + '/deep/nesting/works/[[files]]+': RouteRecordInfo< |
| 33 | + '/deep/nesting/works/[[files]]+', |
| 34 | + '/deep/nesting/works/:files*', |
| 35 | + { files?: ParamValueZeroOrMore<true> }, |
| 36 | + { files?: ParamValueZeroOrMore<false> } |
| 37 | + > |
| 38 | +} |
| 39 | + |
| 40 | +describe('Route Location types', () => { |
| 41 | + it('RouteLocationNormalized', () => { |
| 42 | + function withRoute( |
| 43 | + fn: ( |
| 44 | + to: RouteLocationNormalizedTypedList<RouteNamedMap>[keyof RouteNamedMap] |
| 45 | + ) => void |
| 46 | + ): void |
| 47 | + function withRoute<Name extends keyof RouteNamedMap>( |
| 48 | + name: Name, |
| 49 | + fn: (to: RouteLocationNormalizedTypedList<RouteNamedMap>[Name]) => void |
| 50 | + ): void |
| 51 | + function withRoute<Name extends RouteRecordName>(...args: unknown[]) {} |
| 52 | + |
| 53 | + withRoute('/[name]', to => { |
| 54 | + expectTypeOf(to.params).toEqualTypeOf<{ name: string }>() |
| 55 | + expectTypeOf(to.params).not.toEqualTypeOf<{ notExisting: string }>() |
| 56 | + expectTypeOf(to.params).not.toEqualTypeOf<{ other: string }>() |
| 57 | + }) |
| 58 | + |
| 59 | + withRoute('/[name]' as keyof RouteNamedMap, to => { |
| 60 | + // @ts-expect-error: no all params have this |
| 61 | + to.params.name |
| 62 | + if (to.name === '/[name]') { |
| 63 | + to.params.name |
| 64 | + // @ts-expect-error: no param other |
| 65 | + to.params.other |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + withRoute(to => { |
| 70 | + // @ts-expect-error: not all params object have a name |
| 71 | + to.params.name |
| 72 | + // @ts-expect-error: no route named like that |
| 73 | + if (to.name === '') { |
| 74 | + } |
| 75 | + if (to.name === '/[name]') { |
| 76 | + expectTypeOf(to.params).toEqualTypeOf<{ name: string }>() |
| 77 | + // @ts-expect-error: no param other |
| 78 | + to.params.other |
| 79 | + } |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments