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:
Qjuh
2026-01-27 17:47:56 +01:00
committed by GitHub
parent 323d8e7571
commit 7a7fecbe3c
4 changed files with 41 additions and 9 deletions

View File

@@ -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;
}