npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-coretypes

v0.0.8

Published

A minimal library implementing many core data types

Downloads

13

Readme

Coretypes - Everyday Typescript Data Types

Why this library

Javascript is a minialist language. It has no standard library. ES6 only includes about 50 types as part of its specification, most of which are pretty obscure object types like Uint32Array. Typescript adds another 16 utility types. By contrast, the Java SE runtime has thousands of classes and other runtimes like Python or the CLR are similarly extensive.

One side effect of this minimalism is that Javascript and Typescript developers mostly make do with a small number of primitive types to represent data. One number type represents integers and decimals. Strings are often used to represent flags. Specialized structures like UUIDs and URLs are just represented by strings. For the most part this is a good thing. It leads to concise code, easy serialization and less Java-style proliferation of classes. But it has issues.

Design

The goal of coretypes is to force you to think about typing and validity, even when you are using primitive types to represent data.

Installation

# NPM
npm install ts-coretypes

# YARN
yarn add ts-coretypes

Types

Table of Contents

Numeric Types

String Types

Date and Time Types

Array Types

Int

Type representing an Integer. Implemented as a branded type of number.

As with all Coretypes branded types, there is no compile-time guarentee that an Int is valid. To ensure that an Int is valid you can use the asInt and isInt utility functions, which will fail at runtime rather than allowing an invalid cast.

Note that because of Javascript's double precision floating point number representation, addition and multiplication on integer operations are safe between values of -(2^52 - 1) and 2^53 + 1.

Usage:

import { Int, asInt } from 'ts-coretypes'

const i: Int = asInt(10)
const n: number = 10

function doubleANumber(x: number): number {
    return x + x
}

function doubleAnInt(x: Int): Int {
    return x + x as Int
}

// works
doubleANumber(i)
doubleAnInt(i)
doubleANumber(n)

// Will not compile
doubleAnInt(n)

PositiveNumber

Type representing a positive real number. Implemented as a branded type of number.

As with all Coretypes branded types, there is no compile-time guarentee that an PositiveNumber is valid. To ensure that a PositiveNumber is valid you can use the asPositive and isPositive utility functions, which will fail at runtime rather than allowing an invalid cast.

Usage:

import { PositiveNumber, asPositiveNumber } from 'ts-coretypes'
const p: PositiveNumber = asPositiveNumber(10.4)
const n: number = 10.4

function doubleANumber(x: number): number {
    return x + x
}

function doubleAPositiveNumber(x: PositiveNumber): PositiveNumber {
    return x + x as PositiveNumber
}

// works
doubleANumber(p)
doubleAPositiveNumber(ip
doubleANumber(n)

// Will not compile
doubleAPositiveNumber(n)

NegativeNumber

NonNegativeNumber

PositiveInt

NegativeInt

NonNegativeInt

UUID

NonEmptyString