JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q22143F
  • License MIT

Determine wether something is an object, with TypeScript support.

Package Exports

  • isobject-ts
  • isobject-ts/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (isobject-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

isobject-ts NPM version NPM monthly downloads NPM total downloads

Return true, if a given value is an object. Uses a type predicate for Typescript users to return additional type information. The type returned is Record<string | number | symbol, unknown>.

Install

Install using npm:

$ npm install --save isobject-ts

Usage

import isObject from "isobject-ts"

All of the following return true:

isObject({})
isObject(Object.create({}))
isObject(Object.create(Object.prototype))
isObject(Object.create(null))
isObject({})
isObject(new Foo())
isObject(/foo/)

All of the following return false:

isObject(function () {})
isObject(1)
isObject([])
isObject(undefined)
isObject(null)

You can use the type information like this:

const foo(bar: unknown) => {
    if(!isObject(bar)) return undefined
    /* Type is now inferred to be Record<string | number | symbol, unknown>, 
       so we can now check for keys we want to access */
    const hasBaz = 'baz' in bar
    if(!hasBaz) return undefined
    // We know that our key "baz" is in the object, so we can safely access it
    return bar.baz
}

Author

Tom Heinemeyer

License

Copyright © 2024, Tom Heinemeyer. Released under the MIT License.