Live editor
Press Run or Ctrl/⌘ + Enter.
The playground loads only the shared browser-safe entry. Node adapter examples run in Node and are documented on the reference page.
Recipes
Load a focused example.
Strict and weak modes
JavaScript
import Is from 'strong-type';
const is=new Is;
const weakIs=new Is(false);
console.log(is.string('type'));
console.log(weakIs.number('42'));
try{
is.number('42');
}catch(error){
console.error(error.message);
}Unions and collections
JavaScript
import Is from 'strong-type';
const is=new Is;
const values=[new Map,new Set,'type',42];
for(const value of values){
console.log(
value,
is.union(value,'map|set|string|number')
);
}Extend the validator
JavaScript
import Is from 'strong-type';
class Pizza{}
class MyIs extends Is{
pizza(value){
return this.instanceCheck(value,Pizza);
}
}
const is=new MyIs;
console.log(is.pizza(new Pizza));
console.log(is.union(new Pizza,'pizza|string'));Same pattern in your page
A normal module script is enough.
strong-type works with bundlers and without a bundler. Bundlers resolve strong-type normally; native browser ESM uses the import map below, with no build or transpilation step.
HTML
<script type="importmap">
{
"imports": {
"strong-type": "./node_modules/strong-type/index.js"
}
}
</script>
<script type="module">
import Is from 'strong-type';
const is=new Is(false);
document.body.dataset.valid=String(is.string('strong-type'));
</script>The mapped path is relative to the HTML document. Serve the app over HTTP(S), configure the server to expose the mapped node_modules file, and do not use file://. There are no runtime dependency mappings or scopes to add. For this checkout’s live playground, run npm start and open http://localhost:8000/playground.html.