Skip to content

Fix TS2300: Duplicate identifier errors caused by generated types #13499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/witty-brooms-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@react-router/dev": patch
---

Fix `TS2300: Duplicate identifier` errors caused by generated types

Previously, routes that had the same full path would cause duplicate entries in the generated types for `href` (`.react-router/types/+register.ts`), causing type checking errors.
62 changes: 30 additions & 32 deletions packages/react-router-dev/typegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { Context } from "./context";
import { getTypesDir, getTypesPath } from "./paths";
import * as Params from "./params";
import * as Route from "./route";
import type { RouteManifestEntry } from "../config/routes";

export async function run(rootDirectory: string, { mode }: { mode: string }) {
const ctx = await createContext({ rootDirectory, mode, watch: false });
Expand Down Expand Up @@ -108,44 +109,41 @@ function register(ctx: Context) {

const { t } = Babel;

const indexPaths = new Set(
Object.values(ctx.config.routes)
.filter((route) => route.index)
.map((route) => route.path)
);
const fullpaths: Record<string, RouteManifestEntry[]> = {};
for (const route of Object.values(ctx.config.routes)) {
// skip pathless (layout) routes
if (route.id !== "root" && !route.path) continue;

const lineage = Route.lineage(ctx.config.routes, route);
const fullpath = Route.fullpath(lineage);
const existing = fullpaths[fullpath];
if (!existing || existing.length < lineage.length) {
fullpaths[fullpath] = lineage;
}
}

const typeParams = t.tsTypeAliasDeclaration(
t.identifier("Params"),
null,
t.tsTypeLiteral(
Object.values(ctx.config.routes)
.map((route) => {
// filter out pathless (layout) routes
if (route.id !== "root" && !route.path) return undefined;

// filter out layout routes that have a corresponding index
if (!route.index && indexPaths.has(route.path)) return undefined;

const lineage = Route.lineage(ctx.config.routes, route);
const fullpath = Route.fullpath(lineage);
const params = Params.parse(fullpath);
return t.tsPropertySignature(
t.stringLiteral(fullpath),
t.tsTypeAnnotation(
t.tsTypeLiteral(
Object.entries(params).map(([param, isRequired]) => {
const property = t.tsPropertySignature(
t.stringLiteral(param),
t.tsTypeAnnotation(t.tsStringKeyword())
);
property.optional = !isRequired;
return property;
})
)
Object.keys(fullpaths).map((fullpath) => {
const params = Params.parse(fullpath);
return t.tsPropertySignature(
t.stringLiteral(fullpath),
t.tsTypeAnnotation(
t.tsTypeLiteral(
Object.entries(params).map(([param, isRequired]) => {
const property = t.tsPropertySignature(
t.stringLiteral(param),
t.tsTypeAnnotation(t.tsStringKeyword())
);
property.optional = !isRequired;
return property;
})
)
);
})
.filter((x): x is Babel.Babel.TSPropertySignature => x !== undefined)
)
);
})
)
);

Expand Down