mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
fix(structures): correctly check if flags are present (#11404)
* fix(structures): correctly check if flags are present * chore: isFieldSet type guard --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,3 +1,32 @@
|
||||
export function isIdSet(id: unknown): id is bigint | string {
|
||||
return typeof id === 'string' || typeof id === 'bigint';
|
||||
}
|
||||
|
||||
interface TypeMap {
|
||||
bigint: bigint;
|
||||
boolean: boolean;
|
||||
function(...args: any[]): unknown;
|
||||
number: number;
|
||||
object: object;
|
||||
string: string;
|
||||
symbol: symbol;
|
||||
undefined: undefined;
|
||||
}
|
||||
|
||||
export type TypeofType = keyof TypeMap;
|
||||
|
||||
function hasProperty<Value extends object, Key extends string>(
|
||||
data: Value,
|
||||
fieldName: Key,
|
||||
): data is Record<Key, unknown> & Value {
|
||||
return fieldName in data;
|
||||
}
|
||||
|
||||
export function isFieldSet<Value extends object, Key extends string, Type extends TypeofType>(
|
||||
data: Value,
|
||||
fieldName: Key,
|
||||
type: Type,
|
||||
): data is Record<Key, TypeMap[Type]> & Value {
|
||||
// eslint-disable-next-line valid-typeof
|
||||
return hasProperty(data, fieldName) && typeof data[fieldName] === type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user