Skip to content

unifiers matchTypeOf()

Eugene Lazutkin edited this page Apr 3, 2026 · 3 revisions

Matches values based on their JavaScript type (typeof result).

Introduction

This unifier matches values against type names returned by typeof. Can match a single type or multiple types.

import matchTypeOf from 'deep6/unifiers/matchTypeOf.js';
import unify from 'deep6/unify.js';

unify('hello', matchTypeOf('string')); // truthy
unify(42, matchTypeOf('string')); // false
unify(42, matchTypeOf(['string', 'number'])); // truthy

API

matchTypeOf(types)

Arguments:

  • types — a required type name string or array of type name strings. Valid types are: 'string', 'number', 'boolean', 'undefined', 'object', 'function', 'symbol', 'bigint'.

Returns a MatchTypeOf unifier instance.

Class: MatchTypeOf

Extends Unifier.

Properties

  • types — array of type names to match against.

Methods

  • unify(val, ls, rs) — checks if typeof val is in the types array.
    • Returns true if the type matches.
    • Returns false if val is a Variable or type doesn't match.

Examples

import matchTypeOf from 'deep6/unifiers/matchTypeOf.js';
import {match} from 'deep6';

// Match primitive types
const isPrimitive = matchTypeOf(['string', 'number', 'boolean', 'bigint']);
match(42, isPrimitive); // true
match('hello', isPrimitive); // true
match({}, isPrimitive); // false

// Match any object type
const isObject = matchTypeOf('object');
match({}, isObject); // true
match(null, isObject); // true (typeof null === 'object')

Clone this wiki locally