JSPM

ts-tagged

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 382
  • Score
    100M100P100Q83636F
  • License MIT

Base generic type for weak nominal typing in Typescript

Package Exports

  • ts-tagged

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-tagged) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ts-tagged

Defines the Tagged<T, N> generic type letting you created tagged types with base type T and name N. Two variables of a tagged type can only be assigned to each other if they have the same base type and name.

This implements a weak form of nominal typing suitable for types shared across multiple packages.

Installation

npm install -S ts-tagged

Example

import { Tagged } from "ts-tagged";

type Email = Tagged<string, "Email">;
type DisplayName = Tagged<string, "DisplayName">;

const email: Email = "foo@example.com";
const displayName: DisplayName = "Foo Bar";

function sendEmail(recipient: Email) {
  // ...
}

sendEmail(email); // OK
sendEmail(displayName); // Compilation error: the names don't match
sendEmail("bar@example.com"); // OK: Unnamed, but the base type matches
sendEmail(42); // Compilation error: The base type does not match

License

MIT License