Skip to content

Latest commit

 

History

History
162 lines (126 loc) · 5.63 KB

README.md

File metadata and controls

162 lines (126 loc) · 5.63 KB

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?