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

inferred-types

v0.44.0

Published

Functions which provide useful type inference on TS projects

Downloads

19,066

Readme

logo

Overview

A collection of Typescript utilities which try to preserve as much strong and narrow typing as is possible. In many cases, these type utilities will be paired with a runtime function which provides a runtime mirror to help keep both design time types and runtime values in sync.

All types -- as well as any pairing runtime functions -- are intended to be "self documenting" as much as is possible. This means that they will at least have a description comment which your editor should expose as a popover. In some cases, there are also code examples included in these comments to further flesh out intended use.

To keep things DRY, the documentation here will be kept to a minimum. This README is intended more as a high level introduction of scope and structure of this repo than documentation.

Directory Structure

Under the src/ folder you'll find the the following subdirectories:

  • constants - a set of runtime constants that can provide utility to both runtime functions as well as provide foundation for enumerated types.
  • types - this folder represents the heart of the repo in the form of type utilities and is further broken down by an attempt at functional classification that hopefully aides somewhat in feature discovery.
  • runtime - this is where you'll find runtime functions which mutate state while taking care to provide as much as type information that compliments the runtime environment as is possible.

Runtime synchronization with Types

You will find many runtime functions like ensureleading(str, substring) which have a similarly named type (in this case EnsureLeading<TStr, TSubstring>). This is no accident and at some future point there may even be something approximating 1:1 parity.

This connection between the type system and the runtime environment allows both a harmonization of variables across both environments and helps to ensure their consistency but it the runtime environment also often really needs strong type utilities to extract out narrow type definitions in the runtime environment.

Examples

What follows is not meant to be comprehensive set of examples but rather just a sampling that hopefully gives some perspective on the breadth of scope of this library.

String Literals

Casing Conventions

  • use PascalCase<T>, CamelCase<T>, SnakeCase<T>, and KebabCase<T> to convert a string to one of several familiar naming conventions
  • use AllCaps<T> to make all alphanumeric characters into their uppercase variant

Pluralization

Convert the type and value of a variable from it's singular form to it's plural form:

import { pluralize } from "inferred-types";

// "people"
const people = pluralize("person");

Note: not only does this utility provide all major grammatical rules used in conversion but has a dictionary of common exceptions it will use too.

Consistency with Ensure/Strip utilities

  • it's often desireable to ensure that a string starts with or ends with a given string

  • the inverse can also be useful (aka, to ensure a string does not start or end with a given string literal)

  • this library provides EnsureLeading, EnsureTrailing, StripLeading, and StripTrailing utilities to meet these type needs

    import type { EnsureLeading } from "inferred-types";
    // "start-${string}"
    type T1 = EnsureLeading<string, "start-">;
    // "start-with"
    type T2 = EnsureLeading<"start-with", "start-">;
    // "start-with"
    type T3 = EnsureLeading<"with", "start-">;

Character Sets

  • types like Alpha, NumericChar, Whitespace, Consonants, etc. try to represent character set building blocks that we may have to isolate on for our own types definitions

Data Patterns

  • types like Url, IpAddress, CSV, DotPath, Hexadecimal, ZipCode, and DomainName attempt to provide an out of the box type for common data structure patterns we find in the real world

Numeric Literals

  • use the Add<A,B>, Subtract<A,B>, Increment<T>, Decrement<T> utilities to work with numeric types (or even numeric string literals)
  • get the length of a string or tuple with the Length<T> utility
  • ensure an integer value with Integer<T>; ensure a float value with Float<T>
  • compare numeric literals with LessThan<A,B>, LessThanOrEqual<A,B>, and GreaterThan<A,B>
  • ensure a positive number with Abs<T>

Lists

  • Use AfterFirst, First, Last to index into a tuple / list value
  • Use AsArray<T> to ensure an array value for T
  • Use Flatten<T> to flatten an array
  • Use Slice<T,S,E> to take out a subset of elements in a list
  • Use Reverse to reverse a list

Object / Dictionaries

  1. Based on an object's entity values:

    Assume a base type of Obj:

    type Obj = {
        n1: number;
        n2: 2;
        n3: 3;
        success: true;
        s1: string;
        s2: "hello";
    }

    We can get a union of string literals representing the keys on the object whose value extends some value:

    import type { KeysWithValue, KeysWithoutValue } from "inferred-types";
    // "s1" | "s2"
    type S = KeysWithValue<Obj, string>;
    // "success" | "n1" | "n2"
    type N = KeysWithoutValue<Obj, string>;

    though less used, you can also use KeysEqualValue and KeysNotEqualValue for equality matching

    If you'd prefer to mutate the object's type rather than just identify the keys which extend a value you can do this with: WithValue and WithoutValue:

    import type { WithValue, WithoutValue } from "inferred-types";
    // { s1: string; s2: "hello" }
    type S = WithValue<Obj, string>;
    // { success: true; n1: number; n2: 2; n3: 3 }
    type N = WithoutValue<Obj, string>;

Contributing

If you are using this library and would like to take the next step of contributing; that effort is welcome but please do make sure to always provide both runtime and type tests for any code changes which you submit as a pull request.

See the plentiful examples that exist under the tests/ folder for inspiration.

Note: as of 2024 I'm also starting to add "type performance" tests; not an absolute requirement but in general adding something like you'll find in the benches/ folder for any new type utility would be appreciated.

Licensing

This repo is offered under the highly permissive MIT license.