Skip to content

[Bug]: Passing Location objects into createMemoryRouter's initialEntries doesn't type-check #9470

@ShaneDrury

Description

@ShaneDrury

What version of React Router are you using?

6.4.2

Steps to Reproduce

  • Use a Partial<Location> object in initialEntries when creating a memory router:
import { createMemoryRouter } from 'react-router-dom';
const router = createMemoryRouter([], {
  initialEntries: [{ pathname: '/foo', state: { myState: 'bar' } }],
});

Expected Behavior

The code type-checks and works as normal, where the state is accessible in the components with useLocation.

Actual Behavior

The code does not type-check but works as normal, where the state is accessible in the components with useLocation.
The type error is:

TS2322: Type '{ pathname: string; state: { myState: string; }; }' is not assignable to type 'string'.

This seems to be because initialEntries of createMemoryRouter is typed as string[] rather than InitialEntry[] which is what createMemoryHistory uses and initialEntries is passed on to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions