npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@lexriver/data-types

v3.6.5

Published

Check type of a variable, compare two values or objects.

Readme

DataTypes

Package for checking type of a variable

Install

npm install @lexriver/data-types

Import

import {DataTypes} from '@lexriver/data-types'

Usage

DataTypes.isFunction(x:any):boolean

Check if variable is a function

DataTypes.isClass(x:any):boolean

Check if variable is a class

DataTypes.isClassInstance(classInstance:any, className:any):boolean

Check if variable is instance of a class, internally the same as classInstance instanceof className

DataTypes.isDate(date: any): boolean

Check if variable is instance of Date

DataTypes.isObject(o: any): boolean

Check if variable is object

DataTypes.isObjectWithKeys(o:any): boolean

Check if variable is object with keys

DataTypes.isObjectWithKeys({a:'a'}) // true
DataTypes.isObjectWithKeys({}) // false
DataTypes.isObjectWithKeys('some string') // false

DataTypes.isString(x: any): boolean

Check if variable is a string

DataTypes.isNumber(x: any): boolean

Check if variable is a number

DataTypes.isNullOrUndefined(x: any): boolean

Check if variable is null or undefined

DataTypes.isBoolean(x: any): boolean

Check if variable is a boolean

DataTypes.isArray(x: any): boolean

Check if variable is array

DataTypes.isPrimitive(x: any): boolean

Check if variable is undefined or null or boolean or string or symbol

DataTypes.isEqual(x: any, y: any): boolean

Check if two variables are the same

DataTypes.isEqual(1, 1) // true
DataTypes.isEqual(1, '1') // false
DataTypes.isEqual('a', 'a') // true
DataTypes.isEqual('a', '1') // false
DataTypes.isEqual(1.11, 1.11) // true
DataTypes.isEqual(new Date('2019-11-12'), new Date('2019-11-12')) // true
DataTypes.isEqual(new Date('2019-11-12'), new Date('2019-11-13')) // false
const x = new Date()
const y = x
DataTypes.isEqual(x, y) // true
DataTypes.isEqual(
    [1,'1',new Date('2019-11-12')],
    [1,'1',new Date('2019-11-12')]
) //true
DataTypes.isEqual(
    [1,'1',new Date('2019-11-12'), 1],
    [1,'1',new Date('2019-11-12')]
) //false
DataTypes.isEqual(
    [1,'1',new Date('2019-11-12')],
    [1,'1',new Date('2019-11-12')]
) //true
DataTypes.isEqual(
    {a:'aa', b:'bb'},
    {b: 'bb', a: 'aa'}
) //true
DataTypes.isEqual(
    {a:'aa', b:'bb'},
    {a: 'aa', b:1}
) //false
    const x = {
        a:'aa', 
        b:'bb', 
        c: new Date('2019-11-12'), 
        d: [
            1,
            2,
            '3tt', 
            new Date('2019-11-13'), 
            {
                a: 'aa', 
                b: {
                    bb:'bb', 
                    dd:'dd'
                }
            }
        ],
        e: {
            a:'aaa', 
            b: {
                bb:'bb',
                dd: new Date('2019-11-13'),
                ee: 123
            }
        }
    }
    const y = {
        a:'aa', 
        b:'bb', 
        c: new Date('2019-11-12'), 
        d: [
            1,
            2,
            '3tt', 
            new Date('2019-11-13'), 
            {
                a: 'aa', 
                b: {
                    bb:'bb', 
                    dd:'dd'
                }
            }
        ],
        e: {
            a:'aaa', 
            b: {
                bb:'bb',
                dd: new Date('2019-11-13'),
                ee: 123
            }
        }
    }
    DataTypes.isEqual(x,y) // true

DataTypes.isObjectContainsObject(p:{bigObject:Object, smallObject:Object, ignoreCaseInStringValues?:boolean, ignoreEmptySmallObject?:boolean}):boolean

Check if object contains another object

Parameters

  • bigObject:Object - big object to check
  • smallObject:Object - small object
  • ignoreCaseInStringValues?:boolean - ignore case for strings when compare
  • ignoreEmptySmallObject?:boolean - if true the function returns false if small object is empty

Examples

DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:true, c:3, d:false}, 
    smallObject: {a:'a', b:true, c:3}
}) // true
DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:{b1:'b1', b2:'b2'}, c:'c', d:new Date(2019,12,10)}, 
    smallObject: {a:'a', b:{b1:'b1'}, d:new Date(2019,12,10)}
}) // true
DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:{b1:'b1', b2:'b2'}, c:'c'}, 
    smallObject: {}
}) // true

DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:{b1:'b1', b2:'b2'}, c:'c'}, 
    smallObject: {},
    ignoreEmptySmallObject: true
}) // false
DataTypes.isObjectContainsObject({ 
    bigObject: {a:'aaa', b:'b', d:new Date(2019,10,11)}, 
    smallObject: {a: 'AAA'}, 
    ignoreCaseInStringValues: true
}) //true

DataTypes.isObjectContainsObject({ 
    bigObject: {a:'aaa', b:'b', d:new Date(2019,10,11)}, 
    smallObject: {a: 'AAA'}, 
    ignoreCaseInStringValues: false
}) //false
DataTypes.isObjectContainsObject({
    bigObject: {date:new Date(2019,10,11)},
    smallObject: {date:new Date(2019,10,10)}
}) // false

DataTypes.isObjectContainsObject({
    bigObject: {date:new Date(2019,10,11)},
    smallObject: {date:new Date(2019,10,11)}
}) // true

DataTypes.filterObjectByKeys(o: any, keysToCopy: (key: string) => boolean, recursive?: boolean): object

Returns a new object only with keys specified by predicate function

Parameters

  • o: any - object to filter
  • keysToCopy: (key: string) => boolean - function to check each property for object
  • recursive?: boolean - if true, then perform a deep clone with filter

Example

    const date = new Date()
    const input = {
        a: 'a',
        b: 'b',
        _c: '_c',
        _d: 'd',
        e: {
            e1: 'e1',
            _e2: '_e2'
        },
        f: [
            { f1: 'f1' },
            { _f2: 'f2' }
        ],
        g: date,
        _h: date,
        i: 34,
        _j: 45
    }
    const output = DataTypes.filterObjectByKeys(input, k => k[0] !== '_', true)
/*
    output = {
        a: 'a',
        b: 'b',
        e: {
            e1: 'e1',
        },
        f: [
            { f1: 'f1' },
            {}
        ],
        g: date,
        i: 34
    }
*/    

DataTypes.isValueExistsInEnum(value:any, EnumType:any): boolean

Check if value exists in enum

enum EnumForTest{
    First = 'first',
    Second = 'second'
}

DataTypes.isValueExistsInEnum(EnumForTest.First, EnumForTest) // true
DataTypes.isValueExistsInEnum('first', EnumForTest) // true
DataTypes.isValueExistsInEnum('First', EnumForTest) // false

enum EnumForTestNumber{
    First, // =0
    Second // =1
}

DataTypes.isValueExistsInEnum(1, EnumForTestNumber) // true
DataTypes.isValueExistsInEnum(100, EnumForTestNumber) // false
DataTypes.isValueExistsInEnum(undefined, EnumForTestNumber) // false

DataTypes.hasProperty(obj: T, key: string): boolean

Type-safe check if object has property

type TypeA = {a:string}
type TypeB = {b:string}

function func(param1:TypeA|TypeB){
    if('c' in param1){ // no compilation error
        //...
    }    

    if(DataTypes.hasProperty(param1, 'c')){ // compilation error 
        //...
    }
}

DataTypes.hasDefinedProperty(obj: T, key: K): boolean

Type-safe check if object has property and the property value is not undefined. This function performs type narrowing and returns true if the property exists and is not undefined. Note that null values will return true.

Parameters

  • obj: T - object to check
  • key: K - property key to check (must be a valid key of the union type)

Returns

  • boolean - true if property exists and is not undefined, false otherwise

Examples

type TypeA = {a: string, b?: number}
type TypeB = {c: string}

const obj: TypeA | TypeB = {a: 'test', b: 42}

if (DataTypes.hasDefinedProperty(obj, 'a')) {
    // TypeScript knows obj has property 'a' here
    console.log(obj.a) // OK
}
const obj = {a: 'test', b: undefined, c: null}

DataTypes.hasDefinedProperty(obj, 'a') // true
DataTypes.hasDefinedProperty(obj, 'b') // false (undefined)
DataTypes.hasDefinedProperty(obj, 'c') // true (null is not undefined)
const obj = {a: 0, b: '', c: false}

DataTypes.hasDefinedProperty(obj, 'a') // true (0 is defined)
DataTypes.hasDefinedProperty(obj, 'b') // true (empty string is defined)
DataTypes.hasDefinedProperty(obj, 'c') // true (false is defined)

DataTypes.hasDefinedPropertyAndValue(obj: T, key: K): boolean

Type-safe check if object has property and the property value is not undefined and not null. This is stricter than hasDefinedProperty as it also excludes null values.

Parameters

  • obj: T - object to check
  • key: K - property key to check (must be a valid key of the union type)

Returns

  • boolean - true if property exists and is neither undefined nor null, false otherwise

Examples

type TypeA = {a: string, b?: number | null}
type TypeB = {c: string}

const obj: TypeA | TypeB = {a: 'test', b: 42}

if (DataTypes.hasDefinedPropertyAndValue(obj, 'a')) {
    // TypeScript knows obj has property 'a' with a value here
    console.log(obj.a) // OK
}
const obj = {a: 'test', b: undefined, c: null}

DataTypes.hasDefinedPropertyAndValue(obj, 'a') // true
DataTypes.hasDefinedPropertyAndValue(obj, 'b') // false (undefined)
DataTypes.hasDefinedPropertyAndValue(obj, 'c') // false (null)
const obj = {a: 0, b: '', c: false}

DataTypes.hasDefinedPropertyAndValue(obj, 'a') // true (0 is a value)
DataTypes.hasDefinedPropertyAndValue(obj, 'b') // true (empty string is a value)
DataTypes.hasDefinedPropertyAndValue(obj, 'c') // true (false is a value)

type AnyJsonValue

Represents any json value

export type AnyJsonValue =
    | string
    | number
    | boolean
    | null
    | Date
    | { readonly [K in string]: AnyJsonValue }
    | Array<AnyJsonValue>
    | undefined

DataTypes.isValidJsonObject(json: any):json is AnyJsonValue

Check if parameter is valid json object

    const x = {
        a: 'a',
        b: 'b',
        c: {
            c1:'c1',
            c2: 23234,
            c3: new Date()
        },
        d:[
            'd1', 'd2', 'd3'
        ]
    }
    DataTypes.isValidJsonObject(x) // true
    class MyClass{
        constructor(public a:string){
        }
    }

    const x = new MyClass('x')

    DataTypes.isValidJsonObject({a:x}) // false

DataTypes.toJson(o:any)

Convert any object to valid json object via JSON.parse(JSON.stringify(o))

type JsonType<T>

Represents JSON type to use as parameter for function

Example

function expectingJsonType<T>(x:JsonType<T>){
    // here we can be sure that parameter is valid json object
    return JSON.stringify(json) // or save to database, etc
}

type Person = {
    name:string
}
type NonJsonType = {
    fn: ()=>void
}

const person:Person = {
    name: 'John'
}

expectingJsonType(person) // ok

const nonJson:NonJsonType = {
    fn: ()=>{}
}

expectingJsonType(nonJson) // compilation error

type JsonCompatible<T>

Type JsonCompatible is a type that can be safely converted to JSON. This type sometimes works better than JsonType<T>, for example with interfaces.


    function expectingJsonCompatible<T extends JsonCompatible<T>>(data: T){
        console.log(JSON.stringify(data))
    }

    interface User {
        name:string
    }
    let user:User = {name:'a'}
    expectingJsonCompatible(user)
    expectingJsonType(user) // compile error (!)

    interface UserNonJson {
        name:string
        fn:()=>void
    }
    let userNonJson:UserNonJson = {name:'a', fn:()=>{}}
    expectingJsonCompatible(userNonJson) // compile error
    expectingJsonType(userNonJson) // compile error

type PartiallyPartial<T, K extends keyof T>

Make selected fields optional while keeping all other fields unchanged.

type User = {
    id: string
    email: string
    password: string
}

type UserWithOptionalEmailAndPassword = PartiallyPartial<User, 'email' | 'password'>
/*
    Equivalent to:
    {
        id: string;              // unchanged (still required)
        email?: string | undefined;
        password?: string | undefined;
    }
*/

Use cases

  • Making a subset of fields optional in update payloads while keeping identifiers required.

type SomeOptional<T, K extends keyof T>

Alias for PartiallyPartial<T, K>. Make selected fields optional while keeping the rest of the object unchanged.

type User = {
    id: string
    email: string
    username: string
}

type UserWithSomeOptional = SomeOptional<User, 'email' | 'username'>
/*
    Equivalent to:
    {
        id: string;              // unchanged (still required)
        email?: string | undefined;
        username?: string | undefined;
    }
*/

Use cases

  • Optionalizing only the fields you pass in (e.g., partial updates, patch payloads).

type PartialExcept<T, K extends keyof T>

Make all fields optional except a specified subset which remain required.

type User = {
    id: string
    email?: string
    username?: string
}

type Payload = PartialExcept<User, 'id'>
/*
    Equivalent to:
    {
        id: string;              // required
        email?: string | undefined;
        username?: string | undefined;
    }
*/

Use cases

  • Defining payloads where an identifier must be present but other fields are optional.

type SomeRequired<T, K extends keyof T>

Alias for PartialExcept<T, K>. Make all fields optional except the specified keys, which are required.

type User = {
    id: string
    email?: string
    username?: string
}

type Payload2 = SomeRequired<User, 'id' | 'email'>
/*
    Equivalent to:
    {
        id: string;              // required
        email: string;           // required
        username?: string | undefined;
    }
*/

Use cases

  • Ensuring certain fields are present while relaxing the rest.

type AtLeastOne<T, K extends keyof T>

Require that at least one of the specified keys is present. Useful for filters or input where multiple alternative identifiers are allowed.

type User = {
    id: string
    email: string
    username: string
}

type UserLookup = AtLeastOne<User, 'id' | 'email' | 'username'>

function findUser(query: UserLookup){
    // query must contain at least one of: id, email, username
}

findUser({ id: '123' }) // OK
findUser({ email: '[email protected]' }) // OK
findUser({}) // compile error

Notes

  • Other non-listed fields are optional in the resulting type, but at least one of K must be present.