[Bug]: Router type doesn't exist #9915
-
What version of React Router are you using?6.6.2 Steps to ReproduceThat's a little bit hard to reproduce, because for the majority of projects it's not a bug. I worked on a React template which hold strong ESLint rules. One of them enforce to type everything. So when I declared a variable to store import React from 'react';
import ErrorPage from "../Pages/ErrorPage";
import {createBrowserRouter} from "react-router-dom";
// ROUTES must be typed
export const ROUTES = createBrowserRouter([
{
path: "/",
element: <ErrorPage/>,
errorElement: <ErrorPage/>,
},
]); Nonetheless, according to my IDE (JetBrains WebStorm), the type of my variable import React from 'react';
import ErrorPage from "../Pages/ErrorPage";
import {createBrowserRouter, Router} from "react-router-dom";
// Error TS : 'Router' refers to a value, but is being used as a type here. Did you mean 'typeof Router'?
export const ROUTES: Router = createBrowserRouter([
{
path: "/",
element: <ErrorPage/>,
errorElement: <ErrorPage/>,
},
]); import React from 'react';
import ErrorPage from "../Pages/ErrorPage";
import {createBrowserRouter, type Router} from "react-router-dom";
// Error TS : Type 'Router' is not assignable to type '({ basename: basenameProp, children, location: locationProp, navigationType, navigator, static: staticProp, }: RouterProps) => ReactElement<any, string | JSXElementConstructor<any>> | null'. Type 'Router' provides no match for the signature '({ basename: basenameProp, children, location: locationProp, navigationType, navigator, static: staticProp, }: RouterProps): ReactElement<any, string | JSXElementConstructor<any>> | null'.
export const ROUTES: typeof Router = createBrowserRouter([
{
path: "/",
element: <ErrorPage/>,
errorElement: <ErrorPage/>,
},
]); Type Expected BehaviorA type import React from 'react';
import ErrorPage from "../Pages/ErrorPage";
import {createBrowserRouter, type Router} from "react-router-dom";
export const ROUTES: Router = createBrowserRouter([
{
path: "/",
element: <ErrorPage/>,
errorElement: <ErrorPage/>,
},
]); Actual BehaviorI don't found any matching type for my variable |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I've found a workaround to this. You can use the type of the return value of type Router = ReturnType<typeof createBrowserRouter>; |
Beta Was this translation helpful? Give feedback.
-
I agree this is a bit confusing. You're actually importing the So you can import that directly, or just go with the |
Beta Was this translation helpful? Give feedback.
-
Is there any update? |
Beta Was this translation helpful? Give feedback.
I agree this is a bit confusing. You're actually importing the
<Router>
component fromreact-router-dom
there which is why it's giving you the "Router refers to a value" error. What you want is theRouter
type from the underlying@remix-run/router
package which isn't at the moment re-exported throughreact-router-dom
.So you can import that directly, or just go with the
ReturnType
alias presented by @atfzl above.https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBDAnmApnA3nAShArjFKOAXzgDMoIQ4ByAAShRGAA8BaKXAOwHpL9CNANwAoUJFgY4AY0YBDAgCFKAdwDOhHAKKkKVWvOkwOeAlDYATKsJEiANinj8zcALwz5S1RqhazACgBtdDAFAAsALloeGmIAXQBKUTgeHjgAPQB+WwcnU0IAJii-QjcPFAUUZQh1TXyoIJDwqJoY+KSRFLSszqA