Skip to content

Commit e3d9b16

Browse files
refactor: use PageData type for page data assertions
Replaced anonymous object casts with the defined `PageData` type in `app/sitemap.ts` and `app/docs/layout.tsx`. Removed redundant null-coalescing fallbacks `?? {}` in `app/sitemap.ts` as the `PageData` type supports optional properties and subsequent logic uses optional chaining. Imported `PageData` in `app/docs/layout.tsx` to maintain type consistency across the codebase. Co-authored-by: longsizhuo <114939201+longsizhuo@users.noreply.github.com>
1 parent d9f77b8 commit e3d9b16

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

app/docs/layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { PageTree } from "fumadocs-core/server";
77
import { CopyTracking } from "@/app/components/CopyTracking";
88
import { DocsPageViewTracker } from "@/app/components/DocsPageViewTracker";
99
import { cookies } from "next/headers";
10+
import { type PageData } from "@/app/types/doc";
1011

1112
type Locale = "zh" | "en";
1213
type SourcePage = ReturnType<typeof source.getPages>[number];
@@ -77,7 +78,7 @@ function chooseVariant(variants: SourcePage[], locale: Locale): SourcePage {
7778
const originalMatching = variants.find((p) => {
7879
const { suffix } = stripLocaleSuffix(p.slugs);
7980
if (suffix !== null) return false;
80-
const lang = (p.data as { lang?: string }).lang;
81+
const lang = (p.data as PageData).lang;
8182
return lang === locale;
8283
});
8384
if (originalMatching) return originalMatching;

app/sitemap.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ function buildDocsEntry(page: SourcePage): MetadataRoute.Sitemap[number] {
127127
* @returns {Date | undefined} 解析后的 Date 对象,如果找不到或无效则返回 undefined。
128128
*/
129129
function extractDateFromPage(page: SourcePage): Date | undefined {
130-
// (FIX) 使用我们定义的 PageData 类型,而不是 'as' 一个匿名对象
131-
const data = (page.data ?? {}) as PageData;
130+
// 使用我们定义的 PageData 类型
131+
const data = page.data as PageData;
132132

133133
const candidates: DateLike[] = [
134134
data?.updatedAt,
@@ -183,8 +183,8 @@ function sanitizeSlugPath(slugs: string[]): string {
183183
* @returns {boolean} 如果是草稿或隐藏,返回 true。
184184
*/
185185
function isDraftOrHidden(page: SourcePage): boolean {
186-
// [BUILD 修复] 使用我们定义的 PageData 类型来代替 'any'
187-
const d = (page.data ?? {}) as PageData;
186+
// 使用我们定义的 PageData 类型
187+
const d = page.data as PageData;
188188

189189
// 检查顶层或 'frontmatter' 嵌套下的 'draft' 或 'hidden' 标志
190190
return !!(

0 commit comments

Comments
 (0)