Miss any of our Open RFC calls?Watch the recordings here! »

ts-trapper

0.1.0 • Public • Published

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).

Usage

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.

Install

npm i ts-trapper

DownloadsWeekly Downloads

4

Version

0.1.0

License

Apache-2.0

Last publish

Collaborators

  • avatar