From 715db0347cc4256d0a453dc23fe5d2d3bf983fc7 Mon Sep 17 00:00:00 2001 From: bakaneko Date: Wed, 18 Dec 2024 15:41:00 +0900 Subject: [PATCH] copy and return typed value instead of messing with original object --- resources/js/beatmapsets-show/controller.ts | 15 ++++++++++++--- resources/js/interfaces/tag-json.ts | 3 --- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/resources/js/beatmapsets-show/controller.ts b/resources/js/beatmapsets-show/controller.ts index 04ec53a20a5..c4fc7f1fd53 100644 --- a/resources/js/beatmapsets-show/controller.ts +++ b/resources/js/beatmapsets-show/controller.ts @@ -2,7 +2,7 @@ // See the LICENCE file in the repository root for full licence text. import { BeatmapsetJsonForShow } from 'interfaces/beatmapset-extended-json'; -import { TagJsonWithCount } from 'interfaces/tag-json'; +import TagJson from 'interfaces/tag-json'; import UserJson from 'interfaces/user-json'; import { keyBy } from 'lodash'; import { action, computed, makeObservable, observable, runInAction } from 'mobx'; @@ -25,6 +25,16 @@ interface State { showingNsfwWarning: boolean; } +type TagJsonWithCount = TagJson & { count: number }; + +function asTagJsonWithCount(tag: TagJson) { + return { + count: 0, + ...tag, + }; +} + + export default class Controller { @observable hoveredBeatmap: null | BeatmapJsonForBeatmapsetShow = null; @observable state: State; @@ -88,8 +98,7 @@ export default class Controller { const tagMap = new Map(); for (const tag of this.beatmapset.related_tags) { - tag.count = 0; // assign 0 and cast - tagMap.set(tag.id, tag as TagJsonWithCount); + tagMap.set(tag.id, asTagJsonWithCount(tag)); } for (const beatmap of this.beatmapset.beatmaps) { diff --git a/resources/js/interfaces/tag-json.ts b/resources/js/interfaces/tag-json.ts index da857843edc..5d1352a7a4b 100644 --- a/resources/js/interfaces/tag-json.ts +++ b/resources/js/interfaces/tag-json.ts @@ -2,10 +2,7 @@ // See the LICENCE file in the repository root for full licence text. export default interface TagJson { - count?: number; description: string; id: number; name: string; } - -export type TagJsonWithCount = TagJson & Required>;