guards
Type guard utilities for common validation patterns.
This module consolidates reusable type guards that are used throughout the codebase for validating GraphQL schema objects and other entities.
hasArrayProperty()β
Call Signatureβ
function hasArrayProperty<K>(obj, key): obj is Record<K, unknown[]>;
Defined in: guards.ts:111
Type guard to check if an object has an array property.
Type Parametersβ
Kβ
K extends PropertyKey
Parametersβ
objβ
unknown
The object to check
keyβ
K
The property name to look for
Returnsβ
obj is Record<K, unknown[]>
true if the property exists and is an array, false otherwise
Exampleβ
import { hasArrayProperty } from "@graphql-markdown/utils/guards";
if (hasArrayProperty(type, "args")) {
// type.args is now typed as unknown[]
console.log(type.args.length);
}
Call Signatureβ
function hasArrayProperty(obj, key): obj is Record<PropertyKey, unknown[]>;
Defined in: guards.ts:116
Type guard to check if an object has an array property.
Parametersβ
objβ
unknown
The object to check
keyβ
PropertyKey
The property name to look for
Returnsβ
obj is Record<PropertyKey, unknown[]>
true if the property exists and is an array, false otherwise
Exampleβ
import { hasArrayProperty } from "@graphql-markdown/utils/guards";
if (hasArrayProperty(type, "args")) {
// type.args is now typed as unknown[]
console.log(type.args.length);
}
hasFunctionProperty()β
Call Signatureβ
function hasFunctionProperty<K>(
obj,
key,
): obj is Record<K, (args: unknown[]) => unknown>;
Defined in: guards.ts:213
Type guard to check if an object has a function property.
Type Parametersβ
Kβ
K extends PropertyKey
Parametersβ
objβ
unknown
The object to check
keyβ
K
The property name to look for
Returnsβ
obj is Record<K, (args: unknown[]) => unknown>
true if the property exists and is a function, false otherwise
Exampleβ
import { hasFunctionProperty } from "@graphql-markdown/utils/guards";
if (hasFunctionProperty(directive, "descriptor")) {
// directive.descriptor is now typed as a function
directive.descriptor();
}
Call Signatureβ
function hasFunctionProperty(
obj,
key,
): obj is Record<PropertyKey, (args: unknown[]) => unknown>;
Defined in: guards.ts:218
Type guard to check if an object has a function property.
Parametersβ
objβ
unknown
The object to check
keyβ
PropertyKey
The property name to look for
Returnsβ
obj is Record<PropertyKey, (args: unknown[]) => unknown>
true if the property exists and is a function, false otherwise
Exampleβ
import { hasFunctionProperty } from "@graphql-markdown/utils/guards";
if (hasFunctionProperty(directive, "descriptor")) {
// directive.descriptor is now typed as a function
directive.descriptor();
}
hasNonEmptyArrayProperty()β
Call Signatureβ
function hasNonEmptyArrayProperty<K>(obj, key): obj is Record<K, unknown[]>;
Defined in: guards.ts:145
Type guard to check if an object has a non-empty array property.
Type Parametersβ
Kβ
K extends PropertyKey
Parametersβ
objβ
unknown
The object to check
keyβ
K
The property name to look for
Returnsβ
obj is Record<K, unknown[]>
true if the property exists, is an array, and is non-empty, false otherwise
Exampleβ
import { hasNonEmptyArrayProperty } from "@graphql-markdown/utils/guards";
if (hasNonEmptyArrayProperty(type, "args")) {
// type.args is a non-empty array
const firstArg = type.args[0];
}
Call Signatureβ
function hasNonEmptyArrayProperty(
obj,
key,
): obj is Record<PropertyKey, unknown[]>;
Defined in: guards.ts:150
Type guard to check if an object has a non-empty array property.
Parametersβ
objβ
unknown
The object to check
keyβ
PropertyKey
The property name to look for
Returnsβ
obj is Record<PropertyKey, unknown[]>
true if the property exists, is an array, and is non-empty, false otherwise
Exampleβ
import { hasNonEmptyArrayProperty } from "@graphql-markdown/utils/guards";
if (hasNonEmptyArrayProperty(type, "args")) {
// type.args is a non-empty array
const firstArg = type.args[0];
}
hasProperties()β
function hasProperties<K>(obj, ...keys): obj is Record<K, unknown>;
Defined in: guards.ts:82
Type guard to check if an object has multiple specific properties.
Type Parametersβ
Kβ
K extends PropertyKey
Parametersβ
objβ
unknown
The object to check
keysβ
...K[]
The property names to look for
Returnsβ
obj is Record<K, unknown>
true if the object has all the properties, false otherwise
Exampleβ
import { hasProperties } from "@graphql-markdown/utils/guards";
if (hasProperties(type, "name", "description")) {
// Both properties exist on type
console.log(type.name, type.description);
}
hasProperty()β
Call Signatureβ
function hasProperty<K>(obj, key): obj is Record<K, unknown>;
Defined in: guards.ts:48
Type guard to check if an object has a specific property.
Type Parametersβ
Kβ
K extends PropertyKey
Parametersβ
objβ
unknown
The object to check
keyβ
K
The property name to look for
Returnsβ
obj is Record<K, unknown>
true if the object has the property, false otherwise
Exampleβ
import { hasProperty } from "@graphql-markdown/utils/guards";
if (hasProperty(type, "args")) {
// type.args is now accessible
console.log(type.args);
}
Call Signatureβ
function hasProperty(obj, key): obj is Record<PropertyKey, unknown>;
Defined in: guards.ts:53
Type guard to check if an object has a specific property.
Parametersβ
objβ
unknown
The object to check
keyβ
PropertyKey
The property name to look for
Returnsβ
obj is Record<PropertyKey, unknown>
true if the object has the property, false otherwise
Exampleβ
import { hasProperty } from "@graphql-markdown/utils/guards";
if (hasProperty(type, "args")) {
// type.args is now accessible
console.log(type.args);
}
hasStringProperty()β
Call Signatureβ
function hasStringProperty<K>(obj, key): obj is Record<K, string>;
Defined in: guards.ts:179
Type guard to check if an object has a string property.
Type Parametersβ
Kβ
K extends PropertyKey
Parametersβ
objβ
unknown
The object to check
keyβ
K
The property name to look for
Returnsβ
obj is Record<K, string>
true if the property exists and is a string, false otherwise
Exampleβ
import { hasStringProperty } from "@graphql-markdown/utils/guards";
if (hasStringProperty(type, "description")) {
// type.description is now typed as string
console.log(type.description.length);
}
Call Signatureβ
function hasStringProperty(obj, key): obj is Record<PropertyKey, string>;
Defined in: guards.ts:184
Type guard to check if an object has a string property.
Parametersβ
objβ
unknown
The object to check
keyβ
PropertyKey
The property name to look for
Returnsβ
obj is Record<PropertyKey, string>
true if the property exists and is a string, false otherwise
Exampleβ
import { hasStringProperty } from "@graphql-markdown/utils/guards";
if (hasStringProperty(type, "description")) {
// type.description is now typed as string
console.log(type.description.length);
}
isNonEmptyArray()β
function isNonEmptyArray(value): value is unknown[];
Defined in: guards.ts:246
Type guard to check if a value is a non-empty array.
Parametersβ
valueβ
unknown
The value to check
Returnsβ
value is unknown[]
true if the value is an array with at least one element
Exampleβ
import { isNonEmptyArray } from "@graphql-markdown/utils/guards";
if (isNonEmptyArray(values)) {
// values is now typed as unknown[] with at least one element
console.log(values[0]);
}
isTypeObject()β
function isTypeObject(obj): obj is Record<string, unknown>;
Defined in: guards.ts:27
Type guard to check if a value is an object but not an array.
Parametersβ
objβ
unknown
The value to check
Returnsβ
obj is Record<string, unknown>
true if the value is a non-null object but not an array, false otherwise
Exampleβ
import { isTypeObject } from "@graphql-markdown/utils/guards";
if (isTypeObject(value)) {
// value is now typed as Record<string, unknown> and is not an array
// (includes Date, Map, and other object types)
console.log(Object.keys(value));
}