Skip to content
Source
Sign inSign up
TanStack/router

ParsedHistoryState doesn't work with useLocation #3193

Closedguy-ecton(GitHub profile, opens in a new tab) opened this issue 6 comments

Which project does this relate to?

Router

Describe the bug

After the change in https://github.com/TanStack/router/pull/3119 The following code throws error on 1.97.3 (didn't throw error on 1.95.1):

declare module '@tanstack/history' {
  interface HistoryState {
    test?: string
  }
}


//


const { test } = useLocation({
    select: (location) => location.state,
  })
Property 'test' does not exist on type 'ParsedLocation<{ [x: string]: unknown; }>'.

Also the documentation still says HistoryState: https://github.com/TanStack/router/blob/18610d8ebf945a5fd4f44ee97435bec50fdcf054/docs/framework/react/api/router/ParsedLocationType.md?plain=1#L14

https://github.com/TanStack/router/blob/18610d8ebf945a5fd4f44ee97435bec50fdcf054/packages/react-router/src/location.ts#L9

Your Example Website or App

Steps to Reproduce the Bug or Issue

declare module '@tanstack/history' {
  interface HistoryState {
    test?: string
  }
}


//


const { test } = useLocation({
    select: (location) => location.state,
  })

Expected behavior

As with version 1.95.1 this shouldn't throw an error

Screenshots or Videos

No response

Platform

  • Version: 1.97.3

Additional context

No response

  1. You have to extend the HistoryState interface in the @tanstack/react-router module instead of in the @tanstack/history one. Good catch on the docs though, I will work on them now. Also, by chance, are you using pnpm, maybe the way it structures the node_modules folder is the reason why you cannot extend it in the @tanstack/history package.

  2. When I try

    declare module '@tanstack/react-router' {
      interface HistoryState {
        test?: string
      }
    }

    it breaks everything. Like

    src/App.tsx:11:10 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'Navigate'.
    
    11 import { Navigate, RouterProvider, createRouter } from '@tanstack/react-router'

    Anyway, I found this (https://github.com/TanStack/router/discussions/284#discussioncomment-9341612) and will try to check my local setup (I use npm and Vite BTW).

  3. Can you provide a full minimal reproduction? I created a completely new project and added the code above with the fix I mentioned and it works perfectly, I cannot reproduce your issue.

    1. npm create @tanstack/router@latest crashes for me (tried inside an empty folder, tried after creating vite project with react + ts...)
      Log
    npm create @tanstack/router@latest
    Need to install the following packages:
    @tanstack/create-router@1.97.3
    Ok to proceed? (y)
    npm ERR! code ERESOLVE
    npm ERR! ERESOLVE unable to resolve dependency tree
    npm ERR!
    npm ERR! While resolving: undefined@undefined
    npm ERR! Found: react@19.0.0
    npm ERR! node_modules/react
    npm ERR!   peer react@">=18" from @tanstack/react-router@1.97.3
    npm ERR!   node_modules/@tanstack/react-router
    npm ERR!     peerOptional @tanstack/react-router@"^1.97.3" from @tanstack/create-router@1.97.3
    npm ERR!     node_modules/@tanstack/create-router
    npm ERR!       @tanstack/create-router@"1.97.3" from the root project
    npm ERR!     peer @tanstack/react-router@"^1.97.3" from @tanstack/router-devtools@1.97.3
    npm ERR!     node_modules/@tanstack/router-devtools
    npm ERR!       peerOptional @tanstack/router-devtools@"^1.97.3" from @tanstack/create-router@1.97.3
    npm ERR!       node_modules/@tanstack/create-router
    npm ERR!         @tanstack/create-router@"1.97.3" from the root project
    npm ERR!   peer react@"^19.0.0" from react-dom@19.0.0
    npm ERR!   node_modules/react-dom
    npm ERR!     peer react-dom@">=18" from @tanstack/react-router@1.97.3
    npm ERR!     node_modules/@tanstack/react-router
    npm ERR!       peerOptional @tanstack/react-router@"^1.97.3" from @tanstack/create-router@1.97.3
    npm ERR!       node_modules/@tanstack/create-router
    npm ERR!         @tanstack/create-router@"1.97.3" from the root project
    npm ERR!       1 more (@tanstack/router-devtools)
    npm ERR!     peer react-dom@">=18" from @tanstack/router-devtools@1.97.3
    npm ERR!     node_modules/@tanstack/router-devtools
    npm ERR!       peerOptional @tanstack/router-devtools@"^1.97.3" from @tanstack/create-router@1.97.3
    npm ERR!       node_modules/@tanstack/create-router
    npm ERR!         @tanstack/create-router@"1.97.3" from the root project
    npm ERR!   1 more (@tanstack/router-devtools)
    npm ERR!
    npm ERR! Could not resolve dependency:
    npm ERR! peerOptional react@"^18.3.1" from @tanstack/create-router@1.97.3
    npm ERR! node_modules/@tanstack/create-router
    npm ERR!   @tanstack/create-router@"1.97.3" from the root project
    npm ERR!
    npm ERR! Fix the upstream dependency conflict, or retry
    npm ERR! this command with --force or --legacy-peer-deps
    npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
    1. So i created a new vite project, and added tanstack router according to documentation.
    2. Added to src/vite-env.d.ts:
    declare module '@tanstack/react-router' {
      interface HistoryState {
        test?: string
      }
    }
    1. npm run dev - works
    2. npm run build - breaks.
    Logs
    > tsc -b && vite build
    src/main.tsx:3:10 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'RouterProvider'.
    
    3 import { RouterProvider, createRouter } from '@tanstack/react-router'
               ~~~~~~~~~~~~~~
    
    src/main.tsx:3:26 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'createRouter'.
    
    3 import { RouterProvider, createRouter } from '@tanstack/react-router'
                               ~~~~~~~~~~~~
    
    src/routes/__root.tsx:1:10 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'createRootRoute'.
    
    1 import { createRootRoute, Link, Outlet } from '@tanstack/react-router'
               ~~~~~~~~~~~~~~~
    
    src/routes/__root.tsx:1:27 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'Link'.
    
    1 import { createRootRoute, Link, Outlet } from '@tanstack/react-router'
                                ~~~~
    
    src/routes/__root.tsx:1:33 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'Outlet'.
    
    1 import { createRootRoute, Link, Outlet } from '@tanstack/react-router'
                                      ~~~~~~
    
    src/routes/about.lazy.tsx:1:10 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'createLazyFileRoute'.
    
    1 import { createLazyFileRoute } from '@tanstack/react-router'
               ~~~~~~~~~~~~~~~~~~~
    
    src/routes/index.lazy.tsx:1:10 - error TS2305: Module '"@tanstack/react-router"' has no exported member 'createLazyFileRoute'.
    
    1 import { createLazyFileRoute } from '@tanstack/react-router'
               ~~~~~~~~~~~~~~~~~~~
    
    ❯ node --version
    v20.12.2
    ❯ npm --version
    10.5.0

    (sorry, I'm unable to open a repro right now).

    package.json
    {
      "name": "test-proj",
      "private": true,
      "version": "0.0.0",
      "type": "module",
      "scripts": {
        "dev": "vite",
        "build": "tsc -b && vite build",
        "lint": "eslint .",
        "preview": "vite preview"
      },
      "dependencies": {
        "@tanstack/react-router": "^1.97.3",
        "react": "^18.3.1",
        "react-dom": "^18.3.1"
      },
      "devDependencies": {
        "@eslint/js": "^9.17.0",
        "@tanstack/router-devtools": "^1.97.3",
        "@tanstack/router-plugin": "^1.97.3",
        "@types/react": "^18.3.18",
        "@types/react-dom": "^18.3.5",
        "@vitejs/plugin-react": "^4.3.4",
        "eslint": "^9.17.0",
        "eslint-plugin-react-hooks": "^5.0.0",
        "eslint-plugin-react-refresh": "^0.4.16",
        "globals": "^15.14.0",
        "typescript": "~5.6.2",
        "typescript-eslint": "^8.18.2",
        "vite": "^6.0.5"
      }
    }
  4. ferretwithaberet(GitHub profile, opens in a new tab)commented

    3. Added to src/vite-env.d.ts:

    That declare module is supposed to be in main.tsx.

    Edit: Also, npm create @tanstack/router --legacy-peer-deps works, it's due to the way npm handles peer dependencies, if it does not like something it just errors. Hence why I use pnpm.

  5. That makes sense 😬 as it already contains override for interface Register... Thanks and sorry (at least we'll get the documentation fixed.)

Sign in to join this conversation.