Skip to content

Commit f1adc53

Browse files
fix(build): pnpm build报错Error: src/components/score/ScoreCard.tsx(111,45): error TS18048: 'payload.scores' is possibly 'undefined'.
问题原因是 TypeScript 的类型收窄(narrowing)无法穿透到 .filter() 的回调闭包中。改为把 payload.scores 提前存入局部变量 scores,收窄后的类型在整个函数作用域内都有效。
1 parent fdb79e5 commit f1adc53

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/components/score/ScoreCard.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ function findUserRank(
102102
username: string,
103103
levelIndex: number
104104
): { rank: number; total: number } | null {
105-
if (!payload?.scores || !Array.isArray(payload.scores)) return null;
105+
const scores = payload?.scores;
106+
if (!scores || !Array.isArray(scores)) return null;
106107

107108
const target = username.trim().toLowerCase();
108109
const candidateIndexes = [
109110
levelIndex,
110-
...payload.scores.map((_, index) => index).filter((index) => index !== levelIndex),
111-
].filter((index) => index >= 0 && index < payload.scores.length);
111+
...scores.map((_, index) => index).filter((index) => index !== levelIndex),
112+
].filter((index) => index >= 0 && index < scores.length);
112113

113114
for (const index of candidateIndexes) {
114-
const scoreList = payload.scores[index];
115+
const scoreList = scores[index];
115116
if (!Array.isArray(scoreList)) continue;
116117

117118
const rankIndex = scoreList.findIndex(

0 commit comments

Comments
 (0)