I am currently building a component that accepts some routes, and makes a stepper with a nested router view.
I'm having some trouble with satisfying TypeScript.
I am using vue-router's RouteLocationRaw type.
RouteLocationRaw is a union of string and two intersection types defined as
export declare type RouteLocationRaw = string | (RouteQueryAndHash & LocationAsPath & RouteLocationOptions) | (RouteQueryAndHash & LocationAsRelativeRaw & RouteLocationOptions);
export declare interface RouteQueryAndHash {
query?: LocationQueryRaw;
hash?: string;
}
export declare interface LocationAsPath {
path: string;
}
export declare interface RouteLocationOptions {
replace?: boolean;
force?: boolean;
state?: HistoryState;
}
export declare interface LocationAsRelativeRaw {
name?: RouteRecordName;
params?: RouteParamsRaw;
}
What I would like to do is compare the name of the current route against those passed into the component like so
const activeRoute = computed(() => props.routes.find((propRoute) => propRoute.name === route.name))
This logic works as I would like, but TypeScript complains. With the approach above I get the following errors.
Property 'name' does not exist on type 'RouteLocationRaw'.
Property 'name' does not exist on type 'string'.
TypeScript seems to automatically assume that the type is a string which is the first part of the union. Already kind of a strange thing to assume, but narrowing beyond a string isn't helpful either.
If I add a case for handling routes that are of type string TypeScript still doesn't recognize that name could be a property of route.
Property 'name' does not exist on type '(RouteQueryAndHash & LocationAsPath & RouteLocationOptions) | (RouteQueryAndHash & LocationAsRelativeRaw & RouteLocationOptions)'.
Property 'name' does not exist on type 'RouteQueryAndHash & LocationAsPath & RouteLocationOptions'.