Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): let property of RouteParams optional (fix #1184) #1186

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/RouterLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ function includesParams(
} else {
if (
!Array.isArray(outerValue) ||
outerValue.length !== innerValue.length ||
innerValue.some((value, i) => value !== outerValue[i])
outerValue.length !== innerValue?.length ||
innerValue?.some((value, i) => value !== outerValue[i])
)
return false
}
Expand Down
9 changes: 8 additions & 1 deletion src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ export function isSameRouteLocationParams(
if (Object.keys(a).length !== Object.keys(b).length) return false

for (const key in a) {
if (!isSameRouteLocationParamsValue(a[key], b[key])) return false
const aValue = a[key],
bValue = b[key]
if (
aValue === undefined ||
bValue === undefined ||
!isSameRouteLocationParamsValue(aValue, bValue)
)
return false
}

return true
Expand Down
40 changes: 30 additions & 10 deletions src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RouteRecordName,
_RouteRecordProps,
RouteRecordRedirect,
RouteParams,
} from '../types'
import { createRouterError, ErrorTypes, MatcherError } from '../errors'
import { createRouteRecordMatcher, RouteRecordMatcher } from './pathMatcher'
Expand Down Expand Up @@ -231,6 +232,19 @@ export function createRouterMatcher(
location: Readonly<MatcherLocationRaw>,
currentLocation: Readonly<MatcherLocation>
): MatcherLocation {
// cannot use Required<RouteParams>
type RouteParamsRequired = {
[P in keyof RouteParams]: NonNullable<RouteParams[string]>
}
const removeUndefinedFromRouteParams = (
obj: RouteParams
): RouteParamsRequired => {
for (const key in obj) {
if (obj[key] === undefined) delete obj[key]
}
return obj as RouteParamsRequired
}

let matcher: RouteRecordMatcher | undefined
let params: PathParams = {}
let path: MatcherLocation['path']
Expand All @@ -245,15 +259,17 @@ export function createRouterMatcher(
})

name = matcher.record.name
params = assign(
// paramsFromLocation is a new object
paramsFromLocation(
currentLocation.params,
// only keep params that exist in the resolved location
// TODO: only keep optional params coming from a parent record
matcher.keys.filter(k => !k.optional).map(k => k.name)
),
location.params
params = removeUndefinedFromRouteParams(
assign(
// paramsFromLocation is a new object
paramsFromLocation(
currentLocation.params,
// only keep params that exist in the resolved location
// TODO: only keep optional params coming from a parent record
matcher.keys.filter(k => !k.optional).map(k => k.name)
),
location.params
)
)
// throws if cannot be stringified
path = matcher.stringify(params)
Expand Down Expand Up @@ -291,7 +307,11 @@ export function createRouterMatcher(
name = matcher.record.name
// since we are navigating to the same location, we don't need to pick the
// params like when `name` is provided
params = assign({}, currentLocation.params, location.params)
params = assign(
{},
removeUndefinedFromRouteParams(currentLocation.params),
removeUndefinedFromRouteParams(location.params ?? {})
)
path = matcher.stringify(params)
}

Expand Down
4 changes: 3 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export type RouteParamValue = string
* @internal
*/
export type RouteParamValueRaw = RouteParamValue | number | null | undefined
export type RouteParams = Record<string, RouteParamValue | RouteParamValue[]>
export type RouteParams = {
[P in string]?: RouteParamValue | RouteParamValue[]
}
export type RouteParamsRaw = Record<
string,
RouteParamValueRaw | Exclude<RouteParamValueRaw, null | undefined>[]
Expand Down
7 changes: 7 additions & 0 deletions test-dts/routeParams.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RouteParams, RouteParamsRaw, expectType } from './index'

const params: RouteParams | RouteParamsRaw = { name: 'value' }
// @ts-expect-error
expectType<undefined>(params.nonExist)
// @ts-expect-error
expectType<string>(params.name)