Minimal, runtime-safe type checks for modern Vanilla JS
npm install nanotypestypeof, instanceof, and structural checks.
typeof and instanceof matching
Map, URL, etc.)
window.__DEV__
assertType. versions
bash
npm install nanotypes
`
---
Usage
`js
import { is, assertType, describe } from 'nanotypes';
if (is.string("hello")) {
console.log("It's a string!");
}
if (is(someValue, HTMLElement)) {
someValue.focus();
}
if (is.textNode(document.createTextNode("x"))) {
// Safe to access nodeValue, etc.
}
assertType.promise(Promise.resolve()); // throws TypeError if invalid
console.log(describe.value(new Map())); // "Map"
`
---
API
$3
`ts
is(value, Class)
`
* Uses instanceof internally
* Logs warning in dev if mismatched (via window.__DEV__ = true)
$3
Over 60 guards are exposed automatically:
| Guard | Description |
| ----------------------- | ---------------------------------------- |
| is.string(x) | typeof x === "string" |
| is.numberSafe(x) | Safe number (non-NaN) |
| is.boolean(x) | Boolean primitive |
| is.defined(x) | Not null or undefined |
| is.nullish(x) | null or undefined |
| is.array(x) | Array literal check |
| is.object(x) | Non-null object, not array |
| is.objectStrict(x) | Exactly a {} object |
| is.plainObject(x) | Object with prototype Object or null |
| is.func(x) | Function check |
| is.map(x) | Instance of Map |
| is.date(x) | Instance of Date |
| is.error(x) | Instance of Error |
| is.textNode(x) | DOM Text node |
| is.htmlElement(x) | HTMLElement node |
| is.contentEditable(x) | Editable DOM node |
| is.positiveNumber(x) | Greater than 0 |
| is.negativeNumber(x) | Less than 0 |
| is.integer(x) | Whole number |
| is.finite(x) | Not Infinity, not NaN |
| is.truthy(x) | Coerces to true |
| is.falsy(x) | Coerces to false |
$3
All is. functions have an assertType. equivalent:
`ts
assertType.url(x) // throws TypeError if not a URL
``