1
0
Fork 1
mirror of https://example.com synced 2024-11-22 11:56:38 +09:00

chore: add utility for unsafe type casting

This commit is contained in:
naskya 2024-01-25 05:23:39 +09:00
parent c44121c575
commit 290d9fcbc0
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
3 changed files with 10 additions and 6 deletions

View file

@ -1,10 +1,12 @@
import { unsafeCast } from "./unsafe-cast.js";
export type Promiseable<T> = {
[K in keyof T]: Promise<T[K]> | T[K];
};
export async function awaitAll<T>(obj: Promiseable<T>): Promise<T> {
const target = {} as T;
const keys = Object.keys(obj) as unknown as (keyof T)[];
const keys = unsafeCast<(keyof T)[]>(Object.keys(obj));
const values = Object.values(obj) as any[];
const resolvedValues = await Promise.all(

View file

@ -0,0 +1,4 @@
// unsafe type cast
export function unsafeCast<T>(val: unknown): T {
return val as T;
}

View file

@ -1,6 +1,8 @@
import { Users } from "@/models/index.js";
import { publishInternalEvent } from "@/services/stream.js";
import define from "../../define.js";
import type { EmojiModPerm } from "@/models/entities/user.js";
import { unsafeCast } from "@/prelude/unsafe-cast.js";
export const meta = {
tags: ["admin"],
@ -31,12 +33,8 @@ export default define(meta, paramDef, async (ps) => {
);
}
const _emojiModPerm =
(ps.emojiModPerm as "unauthorized" | "add" | "mod" | "full") ??
"unauthorized";
await Users.update(user.id, {
emojiModPerm: _emojiModPerm,
emojiModPerm: unsafeCast<EmojiModPerm>(ps.emojiModPerm),
});
publishInternalEvent("userChangeModeratorState", {