Skip to content
/ is-what Public
forked from mesqueeb/is-what

❓ Collection of tiny type introspection helpers

License

Notifications You must be signed in to change notification settings

jcbhmr/is-what

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Is what?

👨‍🔬 Collection of tiny type introspection helpers

🦐 Very small
🌳 Tree-shakeable
✅ Fully TypeScript!
🌐 Works in any environment
📦 No dependencies

Installation

npm Yarn pnpm jsDelivr

You can install this package locally using npm, Yarn, or pnpm.

npm install is-what

If you're using Deno, you can import this package either from deno.land/x or ESM>CDN. You can also import it directly from npm using Deno's new npm: prefix.

import {} from "https://deno.land/x/is_what/mod.ts";
import {} from "https://esm.sh/is-what";
import {} from "npm:is-what";

If you're importing this package directly from a browser environment, you can use an npm CDN like ESM>CDN or jsDelivr.

import {} from "https://esm.sh/is-what";
import {} from "https://esm.run/is-what";

Usage

🚀 Here's a basic demo of what this package can help you achieve! To get the full documentation and see how each function inspects the type of a value, check out our 📚 documentation website! You can also fire up VS Code or your favorite IDE and get some awesome auto-complete with JSDoc comments to read right in your editor! ❤️

import { isPlainObject } from "is-what";

console.log(isPlainObject({ a: 1, b: "hello" }));
//=> true

class Person {
  name: string;
  age: number;
  isAdult: boolean;
  constructor(options: { name: string; age: number }) {
    if (!isPlainObject(options)) {
      throw new TypeError(`${options} is not a plain object`);
    }
    this.name = options.name;
    this.age = options.age;
    this.isAdult = options.age >= 18;
  }
  [Symbol.toStringTag] = "Person";
}

const person = new Person({ name: "John", age: 20 });
console.log(person);
//=> { name: 'John', age: 20, isAdult: true }

console.log(new Person(person));
//=> TypeError: [object Person] is not a plain object

Why?

I built is-what because the existing solutions were all too complex or too poorly built.

I was looking for:

  • A simple way to check any kind of type (including non-primitives)
  • Be able to check if an object is a plain object {} or a special object (like a class instance)
  • Let TypeScript automatically know what type a value is when checking

And that's exactly what is-what is! 😄 You might also want to check out some other packages like the Node.js node:util/types module, is-plain-obj, and type-fest for more type checking utilities.

😊 Check out my other TypeScript packages!

Development

TypeScript Node.js

This package is written in TypeScript. We use npm to manage our dev dependencies. Instead of using a custom test runner like Jest or Vitest, we are using the node --test and node:test builtin test runner! To get started, just open this repository in your favorite IDE and run npm start. If you don't want to leave your browser, you can use GitHub Codespaces!

npm start

This will start the test watcher. Make and edits you want! 👩‍💻 After you're finished making your changes, you can run npm test to run the whole test suite, and npm pack to build the package. Make sure you also do npm build:deno every-so-often to make sure that our Deno-specific build of this package works!

npm test
npm pack
npm run build:deno