Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including ts-trapper with all npm packages installed. Try it out:

var tsTrapper = require("ts-trapper")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ts-trapper v0.1.0

TypeScript type-wrappers, quickly defined with classes.

TypeScript Type-Wrapper (npm)

A tiny module that gives you the typeWrapper class constructor + static methods bundle, for quick definition of type wrappers.

What are Type Wrappers?

A way of declaring a new type, that is simply a wrapper around another type, to make mix-ups easier to prevent.

For example, declaring a wrapper around string called UserID, to stop User IDs from being mixed up with other strings (like usernames or descriptions).

This is elaborated on in this blog post (TypeScript-specific version to come).


import { typeWrapper } from 'ts-trapper';

// Declare a new type wrapper:
class UserID extends typeWrapper<UserID,number>() {
  private brand: any;  

// Number -> UserID-wrapped Number
const id = UserID.wrap(123);

// 💥 Can't use like a number without unwrapping.
const wrong1 = id * 55;

// 💥 Can't use it like another wrapped type.
const wrong2 = CompanyID.unwrap(id);

function greet(userId: UserID, name: string): string {
  // UserID-wrapped Number -> Number
  const userIdNumber = UserID.unwrap(userId);

  // Unwrapped; fine to use now.
  return "User #" + userIdNumber + ": " + name;

console.log(greet(id, "Bert"));

See the examples/ directory for more.

Inspired by @giuliocanti's original "class constructor" pattern from this TS Playground example.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free