Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

newtype-ts

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

newtype-ts

Implementation of newtypes in TypeScript

  • 0.3.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
256K
decreased by-10.66%
Maintainers
1
Weekly downloads
 
Created

What is newtype-ts?

The newtype-ts package provides utilities for creating and working with newtypes in TypeScript. Newtypes are a way to create distinct types from existing ones without runtime overhead, which can help with type safety and code clarity.

What are newtype-ts's main functionalities?

Creating Newtypes

This feature allows you to create newtypes, which are distinct types derived from existing ones. The example demonstrates creating a newtype for meters and using an isomorphism to wrap and unwrap values.

import { Newtype, iso } from 'newtype-ts';

type Meter = Newtype<{ readonly Meter: unique symbol }, number>;
const meterIso = iso<Meter>();

const distance: Meter = meterIso.wrap(5);
const value: number = meterIso.unwrap(distance);

Using Newtypes with Functions

This feature shows how to use newtypes with functions to ensure type safety. The example defines newtypes for meters and seconds, and a function to calculate speed, ensuring that only the correct types are used.

import { Newtype, iso } from 'newtype-ts';

type Meter = Newtype<{ readonly Meter: unique symbol }, number>;
const meterIso = iso<Meter>();

type Second = Newtype<{ readonly Second: unique symbol }, number>;
const secondIso = iso<Second>();

const speed = (distance: Meter, time: Second): number => meterIso.unwrap(distance) / secondIso.unwrap(time);

const distance: Meter = meterIso.wrap(100);
const time: Second = secondIso.wrap(10);

const result = speed(distance, time); // 10

Combining Newtypes

This feature demonstrates how to combine newtypes to create more complex types. The example shows how to calculate speed from distance and time, and wrap the result in a newtype for speed.

import { Newtype, iso } from 'newtype-ts';

type Meter = Newtype<{ readonly Meter: unique symbol }, number>;
const meterIso = iso<Meter>();

type Second = Newtype<{ readonly Second: unique symbol }, number>;
const secondIso = iso<Second>();

type Speed = Newtype<{ readonly Speed: unique symbol }, number>;
const speedIso = iso<Speed>();

const calculateSpeed = (distance: Meter, time: Second): Speed => speedIso.wrap(meterIso.unwrap(distance) / secondIso.unwrap(time));

const distance: Meter = meterIso.wrap(100);
const time: Second = secondIso.wrap(10);

const speed: Speed = calculateSpeed(distance, time);

Other packages similar to newtype-ts

Keywords

FAQs

Package last updated on 17 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc