Vue3 Router with Typescript: missing RouteConfig

Total
1
Shares

I created a new vue project from @vue/cli $> vue create my-project, activated the Typescript option and router option, and upgraded to vue3 beta with $>vue add vue-next.

Now, $>npm run serve fails with

ERROR in /home/auser/dev/my-project/src/router/index.ts(1,10):
1:10 Module '"../../node_modules/vue-router/dist/vue-router"' has no exported member 'RouteConfig'.

  > 1 | import { RouteConfig, createRouter, createWebHistory } from 'vue-router';
      |          ^
    2 | import Home from '../views/Home.vue'

The entire file is not that long, and RouteConfig is used later on:

//index.ts
import { RouteConfig, createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue'

const routes: Array<RouteConfig> = [
{
  path: '/',
  name: 'Home',
  component: Home
},
{
  path: '/about',
  name: 'About',
  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

Q: What is the correct type of RouteConfig, which I need for createRouter?


Solution

(Since this is pre-release: I am using [email protected])

I ran into the same thing. The correct type for routes array is RouteRecordRaw according to the view-router.d.ts; RouteConfig is for the createRouter argument itself.

But if I typed the array as RouteRecordRaw then i ran into the problem of RouteRecordRaw definition is composed of several others types that look like they are not exported, so typing the array causes more problems than it solves.

I suspect that a better pattern will emerge, but for the moment I typed the routes array as any, which works.

Leave a Reply

Your email address will not be published. Required fields are marked *