Skip to content
On this page

Type Guards

Instances

Type-gaurd methods for elements.

methodparamvalue
isFile()target: unknowntarget is a File
isNode()target: unknowntarget is a Node
isElement()target: unknowntarget is an Element
isHTMLElement()target: unknowntarget is a HTMLElement
isHTMLInputElement()target: unknowntarget is a HTMLInputElement
isHTMLSelectElement()target: unknowntarget is a HTMLSelectElement
isHTMLTextAreaElement()target: unknowntarget is a HTMLTextAreaElement
isHTMLVideoElement()target: unknowntarget is a HTMLVideoElement
isHTMLAnchorElement()target: unknowntarget is a HTMLAnchorElement
isHTMLImageElement()target: unknowntarget is a HTMLImageElement
isHTMLOptionElement()target: unknowntarget is a HTMLOptionElement
isHTMLButtonElement()target: unknowntarget is a HTMLButtonElement
Return value: Boolean

isKeyOf()

Check if a key is included in a readonly array

paramvalue
key: stringThe main element
source: readonly T[]Readonly array of strings
Return value: Boolean

Example

ts
const keyToCheck = 'hello';

const arrayToCheck = ['hello', 'how', 'are', 'you'];

const isKeyPresent = isKeyOf(keyToCheck, arrayToCheck); // true

isFormField()

Checks if an element is a form field element

paramvalue
element: Element / EventTarget / nullelement
Return value: Boolean

isNotEmpty()

This util makes sure there are no null or undefined in the passed value. Useful for type safety when filtering empty elements from an array.

paramvalue
value: T / null / undefinedvalue to check
Return value: Boolean

Example:

ts
const items = [1, null, 4, undefined, 8];

const filteredItemsError: number[] = items.filter((item) => value !== undefined && value !== null); // Type '(number | null | undefined)[]' is not assignable to type 'number[]'.

const filteredItemsSuccess: number[] = items.filter(isNotEmpty); // Success!

Primitives

Type-gaurd methods for primitives

methodparamvalue
isString()value: unknowntype of value is a string
isNumber()value: unknowntype of value is a number
isBigint()value: unknowntype of value is a bigint
isBoolean()value: unknowntype of value is a boolean
isSymbol()value: unknowntype of value is a symbol
isUndefined()value: unknowntype of value is undefined
isNull()value: unknowntype of value is null
isFunction()value: unknowntype of value is a function
isObject()value: unknowntype of value is an object
Return value: Boolean

Released under the ISC License.