Getting Started with JSTC
JSTC (JavaScript Type Checker) provides runtime type validation with configurable protection levels.
What is JSTC?
JSTC validates JavaScript values at runtime against type specifications. Unlike TypeScript, which validates at compile-time, JSTC checks values while your code is running.
ts
import JSTC from "@briklab/lib/jstc";
// Validate a single value (wrap in array for .for())
const isNumber = JSTC.for([42]).check(["number"]);
console.log(isNumber); // true
// Validate multiple values with matching types
const mixedValid = JSTC.for([42, "hello", true]).check(["number", "string", "boolean"]);
console.log(mixedValid); // trueBasic Usage
Using the JSTC Singleton
The easiest way to get started is using the JSTC constant:
ts
import JSTC from "@briklab/lib/jstc";
JSTC.setProtectionLevel("boundary");
// Check if values match the given types (in order)
const isValid = JSTC.for([42, "hello", 3.14]).check(["number", "string", "number"]);
console.log(isValid); // true - values match type specificationsCreating Custom Instances
For more control, create a JSTypeChecker instance:
ts
import { JSTypeChecker } from "@briklab/lib/jstc";
const checker = new JSTypeChecker("hardened");
const result = checker.for([null]).check(["null"]);
console.log(result); // trueLive Demo
Console
No logs yet.
Supported Types
JSTC supports the following type names:
"number"- Any number (integer or float)"string"- Text values"boolean"- True or false"object"- Objects (not arrays or null)"array"- Array values"function"- Function values"symbol"- Symbol values"bigint"- BigInt values"undefined"- Undefined values"null"- Null values
Protection Levels
Protection levels control runtime safety behavior:
"none": No protection checks or warnings"boundary": Balanced safety with warning-based guidance"hardened": Defensive mode that throws on invalid usage"sandbox": Boundary behavior plus richer hints
ts
JSTC.setProtectionLevel("hardened");
// Now JSTC validation runs in hardened modeNext Steps
Learn more about JSTC:
- JSTypeChecker Class: Deep dive into the class
- Protection Levels: Understand validation modes
- Supported Types: See all type options