Deprecated utilities package for Sentry JavaScript SDKs - all functionality moved to @sentry/core
—
DEPRECATED: Import all functions from @sentry/core instead of @sentry/utils.
Comprehensive type checking utilities for safe runtime type detection and validation across different JavaScript environments.
Core type checking functions for primitive and common JavaScript types.
/**
* Type guard for plain objects (not arrays, functions, or null)
* @param wat - Value to check
* @returns True if value is a plain object
*/
function isPlainObject(wat: unknown): wat is Record<string, any>;
/**
* Type guard for primitive values (string, number, boolean, null, undefined, symbol, bigint)
* @param wat - Value to check
* @returns True if value is a primitive
*/
function isPrimitive(wat: unknown): wat is Primitive;
/**
* Type guard for string values
* @param wat - Value to check
* @returns True if value is a string
*/
function isString(wat: unknown): wat is string;
/**
* Type guard for RegExp objects
* @param wat - Value to check
* @returns True if value is a RegExp
*/
function isRegExp(wat: unknown): wat is RegExp;
/**
* Type guard for parameterized strings (template literals with placeholders)
* @param wat - Value to check
* @returns True if value is a parameterized string
*/
function isParameterizedString(wat: unknown): wat is string;Usage Examples:
import { isPlainObject, isPrimitive, isString } from "@sentry/core";
function processData(data: unknown) {
if (isString(data)) {
return data.toUpperCase(); // Safe to call string methods
}
if (isPlainObject(data)) {
return Object.keys(data).length; // Safe to treat as object
}
if (isPrimitive(data)) {
return String(data); // Safe to convert primitive
}
return null;
}Type guards for promise-like objects and async operations.
/**
* Type guard for thenable objects (objects with a 'then' method)
* @param wat - Value to check
* @returns True if value is thenable
*/
function isThenable(wat: any): wat is PromiseLike<any>;Usage Example:
import { isThenable } from "@sentry/core";
function handleResult(result: unknown) {
if (isThenable(result)) {
return result.then(value => console.log('Resolved:', value));
} else {
console.log('Synchronous result:', result);
return result;
}
}Type guards for DOM elements and browser-specific objects.
/**
* Type guard for DOM Element objects
* @param wat - Value to check
* @returns True if value is a DOM Element
*/
function isElement(wat: unknown): wat is Element;
/**
* Type guard for Event objects
* @param wat - Value to check
* @returns True if value is an Event
*/
function isEvent(wat: unknown): wat is Event;
/**
* Type guard for synthetic events (React-style events)
* @param wat - Value to check
* @returns True if value appears to be a synthetic event
*/
function isSyntheticEvent(wat: unknown): wat is { [key: string]: any };
/**
* Type guard for Vue.js view model objects
* @param wat - Value to check
* @returns True if value is a Vue view model
*/
function isVueViewModel(wat: unknown): wat is { [key: string]: any };Usage Examples:
import { isElement, isEvent, isSyntheticEvent } from "@sentry/core";
function handleDOMValue(value: unknown) {
if (isElement(value)) {
return value.tagName; // Safe to access Element properties
}
if (isEvent(value)) {
return value.type; // Safe to access Event properties
}
if (isSyntheticEvent(value)) {
// Handle React synthetic events
return value.nativeEvent || value;
}
return null;
}
// Event handler that works with both native and synthetic events
function handleClick(event: unknown) {
if (isEvent(event)) {
event.preventDefault();
} else if (isSyntheticEvent(event)) {
event.preventDefault?.();
}
}Safe instanceof checking that handles edge cases and cross-frame scenarios.
/**
* Safe instanceof check that handles cross-frame scenarios
* @param wat - Object to check
* @param base - Constructor function or object to check against
* @returns True if wat is an instance of base
*/
function isInstanceOf(wat: any, base: any): boolean;Usage Example:
import { isInstanceOf } from "@sentry/core";
function checkInstance(obj: unknown) {
// Safe even across different window/frame contexts
if (isInstanceOf(obj, Error)) {
return obj.message;
}
if (isInstanceOf(obj, Date)) {
return obj.toISOString();
}
return null;
}Utilities for matching values against patterns or regular expressions.
/**
* Checks if a value matches any of the provided patterns
* @param wat - Value to check (will be converted to string)
* @param patterns - Array of strings or RegExp patterns to match against
* @returns True if value matches any pattern
*/
function isMatchingPattern(wat: string, patterns: Array<string | RegExp>): boolean;Usage Example:
import { isMatchingPattern } from "@sentry/core";
const ignoredErrors = [
'Script error',
/network.*error/i,
'ResizeObserver loop limit exceeded'
];
function shouldIgnoreError(errorMessage: string): boolean {
return isMatchingPattern(errorMessage, ignoredErrors);
}
// Usage in error handler
if (shouldIgnoreError(error.message)) {
return; // Skip processing this error
}Type guards can be combined for more complex type checking:
import { isPlainObject, isString, isThenable } from "@sentry/core";
interface User {
id: string;
name: string;
email?: string;
}
function isUser(obj: unknown): obj is User {
return (
isPlainObject(obj) &&
isString(obj.id) &&
isString(obj.name) &&
(obj.email === undefined || isString(obj.email))
);
}
function processUserData(data: unknown) {
if (isUser(data)) {
// TypeScript knows data is User here
console.log(`Processing user: ${data.name}`);
return data;
}
throw new Error('Invalid user data');
}Type guards are useful for runtime validation of API responses and user input:
import { isPlainObject, isString, isThenable } from "@sentry/core";
async function fetchUserData(id: string) {
const response = await fetch(`/api/users/${id}`);
const data = await response.json();
// Validate response structure
if (!isPlainObject(data)) {
throw new Error('Invalid API response: not an object');
}
if (!isString(data.name)) {
throw new Error('Invalid API response: missing name');
}
return data;
}Type guards help with framework-specific type checking:
import { isSyntheticEvent, isVueViewModel } from "@sentry/core";
function handleFrameworkEvent(event: unknown, framework: 'react' | 'vue') {
if (framework === 'react' && isSyntheticEvent(event)) {
// Handle React synthetic event
const nativeEvent = event.nativeEvent;
return nativeEvent;
}
if (framework === 'vue' && isVueViewModel(event)) {
// Handle Vue component event
return event.$el;
}
return event;
}type Primitive =
| null
| undefined
| string
| number
| boolean
| symbol
| bigint;
interface PlainObject {
[key: string]: any;
}Migration Note: All type guard functions have been moved from @sentry/utils to @sentry/core. Update your imports accordingly.
Install with Tessl CLI
npx tessl i tessl/npm-sentry--utils