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-std/types

v1.2.0

Published

Various helpful type definitions and type assertion helpers. Includes many tricky recursive and functional types.

Downloads

143

Readme

@ts-std/types

Various helpful type definitions and type assertion helpers. Includes many tricky recursive and functional types.

API

Tuples

function tuple<L extends any[]>(...values: L): L

Let's you quickly create a tuple without requiring a type assertion.

// the old way
const yuck = [1, 'a', true] as [number, string, boolean]

// the better way
// especially nice if you need to create many tuples in a file
import { tuple as t } from '@ts-std/types'
const yay = t(1, 'a', true)

type Unshift<Item, List extends any[]>

Very useful for prepending items to tuples.

const a: Unshift<number, [string, boolean]> = [1, 'a', true]

function join<T, L extends any[]>(item: T, ...list: L): Unshift<T, L> {
  return [item, ...list] as Unshift<T, L>
}

type Head<L extends any[]>

Gets the first element from a tuple type.

assert_is_type<Head<[number, string], number>>(true)
assert_is_type<Head<[], never>>(true)

type Tail<L extends any[]>

Gets all but the Head from a tuple type.

assert_is_type<Tail<[number, string, boolean], [string, boolean]>>(true)
assert_is_type<Tail<[number], []>>(true)
assert_is_type<Tail<[], []>>(true)

type HasTail<L extends any[]>

Checks whether the tuple type has a tail.

assert_boolean_type<HasTail<[number, string]>>(true)
assert_boolean_type<HasTail<[number]>>(false)
assert_boolean_type<HasTail<[]>>(false)

type Last<L extends any[]>

Gets the final item from a tuple type.

assert_is_type<Last<[number, string]>, string>(true)
assert_is_type<Last<[number]>, number>(true)
assert_is_never<Last<[]>>(true)

type OnlyOne<L extends any[]>

Checks whether a tuple type has only one element.

assert_boolean_type<OnlyOne<[number, string]>>(false)
assert_boolean_type<OnlyOne<[number]>>(true)
assert_boolean_type<OnlyOne<[]>>(false)

Type assertion

These are useful for testing if your library functions return the correct types or not. Typically used in a testing suite to ensure that unexpected type returns break the build.

type IsType<T, U>

Checks that two types are exactly the same.

type IsNever<T> = IsType<T, never>

Checks that a type is never.

function assert_boolean_type<T extends boolean>(expectTrue: T extends true ? true : false)

Checks a boolean conditional type.

// type IsString<T> = T extends string ? true : false
assert_boolean_type<IsString<string>>(true)
assert_boolean_type<IsString<boolean>>(false)

function assert_is_type<T, U>(expectTrue: IsType<T, U> extends true ? true : false)

Checks that two types are exactly the same.

assert_is_type<NonNullable<string | null>, string>(true)
assert_is_type<NonNullable<string | null>, string | null>(false)

function assert_is_never<T>(expectTrue: IsNever<T> extends true ? true : false)

Checks that a type is never.

assert_is_never<Head<[]>>(true)
assert_is_never<Head<[string]>>(false)

function assert_type<T>(value: T)

Checks that the value is of type.

assert_type<number>(4)

function assert_value_types<T, U>(a: T, b: U, expectTrue: IsType<T, U> extends true ? true : false)

Checks that two values have the same type.

assert_value_types(
  [1, 'a', true] as [number, string, boolean],
  t(1, 'a', true),
  true,
)

assert_value_types(
  [1, 'a', true],
  t(1, 'a', true),
  false,
)

Miscellaneous

type KeysOfType<T, U>

Gets the keys from T that are of U.

type Data = { a: number, b: string, c: boolean }
type A = KeysOfType<Data, number> // -> 'a'
type B = KeysOfType<Data, string> // -> 'b'
type Both = KeysOfType<Data, number | string> // -> 'a' | 'b'

type PickOfType<T, U>

Creates a new type from T with only the keys that are of U.

type Data = { a: number, b: string, c: boolean }
type A = PickOfType<Data, number> // -> { a: number }
type B = PickOfType<Data, string> // -> { b: string }
type Both = PickOfType<Data, number | string> // -> { a: number, b: string }

type SingleParameter<F extends (arg: any) => any>

Pulls the single input type from a function.

assert_is_type<SingleParameter<(arg: number) => any, number>>(true)
assert_is_type<SingleParameter<(arg: number) => any>, string>(false)

type FoldingFunctions<L extends ((arg: any) => any)[]>

Requires that a tuple of functions have inputs and outputs that "chain" from one to the next, and gives the final output type.

Useful for situations where functions will be called in order with the output of the previous.

assert_is_type<FoldingFunctions<[() => number, (a: number) => string, (a: string) => boolean]>, boolean>(true)
assert_is_type<FoldingFunctions<[() => number]>, number>(true)
assert_is_type<FoldingFunctions<[() => number]>, string>(false)

assert_is_never<FoldingFunctions<[() => number, () => string]>>(true)
assert_is_never<FoldingFunctions<[]>>(true)
assert_is_never<FoldingFunctions<[() => number, (a: string) => string]>>(true)