A reproduction of well-typed code that fails due to an incorrect type in Vue Router 4.
npm install
npm run typecheck
npm run dev
- Go to
/
and check for errors. - Go to
/?foo=bar
and check for errors.
Step 3 should show that the code does not type-check, as it includes dangerous code that should produce an error in step 4.
Step 3 shows that the code type-checks. The bug, reproduced in step 4, comes from route.query.foo
being undefined
, despite route.query.foo
not being inhabited by undefined
.
The type of LocationQuery
should be changed from:
export type LocationQueryValue = string | null
export type LocationQuery = Record<
string,
LocationQueryValue | LocationQueryValue[]
>
to
export type LocationQueryValue = string | null
export type LocationQuery = Record<
string,
LocationQueryValue | LocationQueryValue[] | undefined
>
LocationQueryRaw
is defined as follows, meaning that it probably does not need to change:
export type LocationQueryValueRaw = LocationQueryValue | number | undefined
export type LocationQueryRaw = Record<
string | number,
LocationQueryValueRaw | LocationQueryValueRaw[]
>