Skip to content

Commit

Permalink
add Nullable utility type
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko committed Dec 16, 2024
1 parent 834705d commit 30119e2
Show file tree
Hide file tree
Showing 17 changed files with 22 additions and 20 deletions.
2 changes: 0 additions & 2 deletions resources/js/artist-tracks-index/search-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { trans } from 'utils/lang';
import { presence, present } from 'utils/string';
import makeLink from './make-link';

type Nullable<T> = T | null | undefined;

type EsRangeField = 'gt' | 'gte' | 'lt' | 'lte';

type EsRange<T> = Partial<Record<EsRangeField, Nullable<T>>>;
Expand Down
2 changes: 1 addition & 1 deletion resources/js/beatmap-discussions/beatmap-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import DiscussionsState from './discussions-state';

interface Props {
discussionsState: DiscussionsState;
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
}

@observer
Expand Down
2 changes: 1 addition & 1 deletion resources/js/beatmap-discussions/beatmaps-owner-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Props {
beatmapset: BeatmapsetExtendedJson;
discussionsState: DiscussionsState;
onClose: () => void;
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
}

@observer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Props {
cannotVote: boolean;
discussion: BeatmapsetDiscussionJsonForShow;
discussionsState: DiscussionsState;
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
}

@observer
Expand Down
4 changes: 2 additions & 2 deletions resources/js/beatmap-discussions/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import React from 'react';
import { trans } from 'utils/lang';

interface Props {
discussions: Map<number | null | undefined, BeatmapsetDiscussionJson>;
discussions: Map<Nullable<number>, BeatmapsetDiscussionJson>;
events: BeatmapsetEventJson[];
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
}

export class Events extends React.PureComponent<Props> {
Expand Down
2 changes: 1 addition & 1 deletion resources/js/beatmap-discussions/review-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function isText(node: UnistNode): node is TextNode {
return node.type === 'text';
}

export function parseFromJson(json: string, discussions: Map<number | null | undefined, BeatmapsetDiscussionJson>) {
export function parseFromJson(json: string, discussions: Map<Nullable<number>, BeatmapsetDiscussionJson>) {
let srcDoc: BeatmapDiscussionReview;

try {
Expand Down
2 changes: 1 addition & 1 deletion resources/js/chat/chat-state-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export default class ChatStateStore implements DispatchListener {
if (mode == null) return;

let hash = '';
const params: Record<'channel_id' | 'sendto', string | null | undefined> = { channel_id: null, sendto: null };
const params: Record<'channel_id' | 'sendto', Nullable<string>> = { channel_id: null, sendto: null };

if (typeof channel === 'string') {
hash = channel;
Expand Down
4 changes: 2 additions & 2 deletions resources/js/components/beatmapset-event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function simpleKebab(str: string | number | undefined) {
export type EventViewMode = 'discussions' | 'profile' | 'list';

interface Props {
discussions?: Map<number | null | undefined, BeatmapsetDiscussionJson>;
discussions?: Map<Nullable<number>, BeatmapsetDiscussionJson>;
event: BeatmapsetEventJson;
mode: EventViewMode;
time?: string;
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
}

export default class BeatmapsetEvent extends React.PureComponent<Props> {
Expand Down
2 changes: 1 addition & 1 deletion resources/js/components/beatmapset-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as React from 'react';
export interface Props {
events: BeatmapsetEventJson[];
mode: EventViewMode;
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
}

export default class BeatmapsetEvents extends React.PureComponent<Props> {
Expand Down
4 changes: 2 additions & 2 deletions resources/js/interfaces/beatmapset-discussions-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import UserJson from 'interfaces/user-json';
export default interface BeatmapsetDiscussionsStore {
beatmaps: Map<number, BeatmapExtendedJson>;
beatmapsets: Map<number, BeatmapsetExtendedJson>;
discussions: Map<number | null | undefined, BeatmapsetDiscussionJson>;
users: Map<number | null | undefined, UserJson>;
discussions: Map<Nullable<number>, BeatmapsetDiscussionJson>;
users: Map<Nullable<number>, UserJson>;
}
2 changes: 1 addition & 1 deletion resources/js/modding-profile/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { trans } from 'utils/lang';
interface Props {
events: BeatmapsetEventJson[];
user: UserJson;
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
}

export default class Events extends React.Component<Props> {
Expand Down
2 changes: 1 addition & 1 deletion resources/js/modding-profile/votes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const directions = ['received', 'given'] as const;
export type Direction = (typeof directions)[number];

interface Props {
users: Map<number | null | undefined, UserJson>;
users: Map<Nullable<number>, UserJson>;
votes: Record<Direction, VoteSummary[]>;
}

Expand Down
4 changes: 4 additions & 0 deletions resources/js/types/nullable.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

type Nullable<T> = T | null | undefined;
2 changes: 1 addition & 1 deletion resources/js/utils/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

export function mobxArrayGet<T>(array: T[] | null | undefined, index: number): T | undefined {
export function mobxArrayGet<T>(array: Nullable<T[]>, index: number): T | undefined {
return array != null && array.length > index ? array[index] : undefined;
}
2 changes: 1 addition & 1 deletion resources/js/utils/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import GroupJson from 'interfaces/group-json';
import { forEach } from 'lodash';
import { present } from './string';

export type Modifiers = (string | null | undefined)[] | Partial<Record<string, boolean | null | undefined>> | string | null | undefined;
export type Modifiers = Nullable<(Nullable<string>)[] | Partial<Record<string, Nullable<boolean>>> | string>;

const eachModifier = (modifiersArray: Modifiers[], callback: (modifier: string) => void) => {
modifiersArray.forEach((modifiers) => {
Expand Down
2 changes: 1 addition & 1 deletion resources/js/utils/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function mapBy<T, K extends keyof T>(array: T[], key: K) {
}

export function mapByWithNulls<T, K extends keyof T>(array: T[], key: K) {
const map = new Map<T[K] | null | undefined, T>();
const map = new Map<Nullable<T[K]>, T>();

for (const value of array) {
map.set(value[key], value);
Expand Down
2 changes: 1 addition & 1 deletion resources/js/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function safeReactMarkdownUrl(url: string | undefined) {
}
}

export function updateQueryString(url: string | null, params: Record<string, string | null | undefined>, hash?: string) {
export function updateQueryString(url: string | null, params: Record<string, Nullable<string>>, hash?: string) {
const docUrl = currentUrl();
const urlObj = new URL(url ?? docUrl.href, docUrl.origin);

Expand Down

0 comments on commit 30119e2

Please sign in to comment.