Skip to content

Centralize PageData type and refactor page.data casts#323

Merged
longsizhuo merged 2 commits intomainfrom
fix-pagedata-type-refactor-1321556333293577153
Apr 27, 2026
Merged

Centralize PageData type and refactor page.data casts#323
longsizhuo merged 2 commits intomainfrom
fix-pagedata-type-refactor-1321556333293577153

Conversation

@longsizhuo
Copy link
Copy Markdown
Member

This PR centralizes the PageData and DateLike type definitions into a single file (app/types/doc.ts) and refactors various parts of the codebase that were using anonymous object casts for page.data. This improves type safety and maintainability by ensuring that all components and utilities interacting with Fumadocs page data use a consistent and well-defined interface.

Key changes:

  1. New Type Definition: Added app/types/doc.ts which defines the PageData interface, including common frontmatter fields, Fumadocs-specific fields, and support for arbitrary metadata.
  2. Sitemap Cleanup: Removed duplicate type definitions from app/sitemap.ts and replaced anonymous casts with the new PageData type.
  3. Search Index Optimization: Replaced the local PageDataShape interface in lib/search-index.ts with the centralized PageData type.
  4. Page Component Refactor: Updated app/docs/[...slug]/page.tsx to use PageData, removing redundant inline type definitions during casting.

PR created automatically by Jules for task 1321556333293577153 started by @longsizhuo

- Created `app/types/doc.ts` to host shared `PageData` and `DateLike` types.
- Updated `app/sitemap.ts` to use imported types instead of local definitions.
- Refactored `lib/search-index.ts` to use `PageData` instead of `PageDataShape`.
- Updated `app/docs/[...slug]/page.tsx` to use `PageData` for `page.data` casts, simplifying the code.
- Ensured consistent property access across the codebase.

Co-authored-by: longsizhuo <114939201+longsizhuo@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
involutionhell-github-io Error Error Apr 27, 2026 3:42pm
website-preview Ready Ready Preview, Comment Apr 27, 2026 3:42pm

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes the typing for Fumadocs page.data into app/types/doc.ts and updates call sites to use the shared PageData/DateLike types instead of local/anonymous casts, improving consistency across sitemap generation, search indexing, and docs rendering.

Changes:

  • Add app/types/doc.ts to define shared PageData and DateLike.
  • Refactor lib/search-index.ts to use the centralized PageData type (including structuredData/load handling).
  • Remove duplicate PageData/DateLike definitions from app/sitemap.ts and update app/docs/[...slug]/page.tsx casts to use the shared type.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
app/types/doc.ts Introduces centralized PageData/DateLike definitions for Fumadocs page.data.
lib/search-index.ts Replaces local page data shape typing with the shared PageData.
app/sitemap.ts Removes duplicated local type definitions and imports shared types.
app/docs/[...slug]/page.tsx Uses shared PageData for lang/docId access instead of ad-hoc casts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/types/doc.ts Outdated
Comment on lines +40 to +45
[key: string]: any;
};
/**
* 允许通过索引访问其他动态属性
*/
[key: string]: any;
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PageData/frontmatter use [key: string]: any, which effectively opts out of type safety for all extra metadata and undermines the PR’s goal of improving typing. Consider using unknown for the index signatures (or replacing them with an explicit metadata?: Record<string, unknown>), so callers must narrow/validate before use while keeping named fields strongly typed.

Suggested change
[key: string]: any;
};
/**
* 允许通过索引访问其他动态属性
*/
[key: string]: any;
[key: string]: unknown;
};
/**
* 允许通过索引访问其他动态属性
*/
[key: string]: unknown;

Copilot uses AI. Check for mistakes.
PR #323 build 挂在 lint:app/types/doc.ts 两处 [key: string]: any
触发 @typescript-eslint/no-explicit-any error。

修法权衡
- frontmatter 嵌套对象:any → unknown(真的动态字段,调用方需显式 narrow)
- PageData 顶层:直接干掉 [key: string]: any 索引签名

为什么不是 unknown:试过 unknown,所有 5 处 `as PageData` cast 都炸
TS2352 "neither type sufficiently overlaps" —— Fumadocs 的 page.data
由 zod DocOut 推出,本身没有 index signature;PageData 顶层挂一个就跟
zod 类型形成"对方有但我没有"的不兼容。要么所有 cast 改 `as unknown as PageData`
(等于把 escape hatch 散到 5 处),要么干脆不挂顶层 index signature
(escape hatch 收窄到一处 frontmatter 子对象)。后者更干净。

调用方(page.tsx / sitemap.ts / search-index.ts)的现有访问字段
(docId / lang / draft / hidden / DateLike 候选)都已经在 PageData 顶层
显式声明,去掉 index signature 不影响任何现有代码,tsc + lint 均过。

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@longsizhuo longsizhuo merged commit 17aa48c into main Apr 27, 2026
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants