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-typetools

v1.1.0

Published

A collection of TypeScript type, type expressions, guard functions, and type-smart identity functions.

Downloads

27,880

Readme

TypeTools are a collection of TypeScript type, type expressions and functions that aid writing idiomatic JavaScript in TypeScript a little easier.

Convenience Type Aliases

  • Falsy represents the type of all falsy values.
  • NullLike represents the null and undefined types.
  • Key represents the type of all valid keys, namely number, string and symbol.
  • Primitive represents the type of all primitive (not object) values, namely string, number, boolean, symbol, null, undefined.
  • Concrete represents the type of everything that is not null or undefined, (the opposite of NullLike).

Convenience Type Expressions

  • MaybePromise<T> is a shortcut for T | Promise<T>.
  • ResolveType<T> obtains the resolve() type of a promise T, or the resolve() type of the return value of a function T, otherwise T itself.
  • MaybeArray<T> is a shortcut for T | Array<T>.
  • ArrayItemType<T> obtains the element type of an array T, otherwise T itself.
  • ExtractRefine<T, U, V> refines part of type T that extends U (like the built-in Extract type) by interesecting that extraction with the V type.
  • ExtractOmit<T, U, K> selectively omits properties K from a part of type T that extends U.
  • ExtractReplace<T, U, V> selectively replaces a type in T that extends U with V.
  • UnionKeyOf<T> obtains the union of the key types of each union component. This is different from applying keyof to a union, which returns an intersection of the key types of each union component. While keyof returns keys that exist in every union component, UnionKeyOf returns keys that existing in at least one (but not necessarily all) union component.
  • OmitStrict<T, K> drops keys K from type T, just like native Omit, except that here the keys are constrained to the union of keys of T (see UnionKeyOf).
  • FitsSub<Sub, Super> gives Sub type back, but will require that it extends Super.
  • FitsSuper<Super, Sub> gives Super type back, but will require that Sub extends from it.

Convenience Guards

  • isConcrete() guards for the Concrete type
  • isTruthy() guards for the truthy type.
  • isEnum(enumObject) produces a function that guards for the enum provided by the enumObject.
  • hasKey(key) produces a function that guards for the subset of values that are objects containing a property with the name specified in key.
  • hasTag(tagOrTags, prop) produces a function that guards for the objects containing a property named as specified in prop, whose value is one of the tags specified. This is used to narrow discriminated unions.

Type-aware utilities

  • not(fn) produces a function that returns the boolean-negated version of the original functin's result. If the given function is a guard, it produces an "anti-guard".
  • sieve(fnMap, tagProp) produces a function that calls a different function on the map based on the tag of input value, which is a discrimated union.
  • asType<T>(value) is an identity function that casts value into a broader type specified in T. This is the same as if we were to save value into a temporary const of type T, and is the opposite of doing value as T, where value could be cast into a narrower type without assertion (as is therefore unsafe).
  • fitsType<T>(value) is an identity function that checks that value is a subtype of T without casting the type of value itself into T.
  • readonly(value) is an identity function that

Test utilities

  • expectType(value) or expectType<T>() can be used to perform type testing, using the quasi-human language and one of the four assertion symbols: ExactType, SubType, SuperType and Unrelated. Examples:
// Use types
expectType<number>().assert<number>().toBe(ExactType);
expectType<number>().assert<2>().toBe(SubType);
expectType<number>().assertBase<2>().toBe(SuperType);
expectType<number>().assert<boolean>().toBe(Unrelated);

// Or use values
expectType(10).assert(10).toBe(ExactType);
expectType(10)
  .assert(2 as const)
  .toBe(SubType);
expectType(10)
  .assertBase(2 as const)
  .toBe(SuperType);
expectType(10).assert(false).toBe(Unrelated);

// Or mix them up
expectType<number>().assert(10).toBe(ExactType);
expectType(10).assert<2>().toBe(SubType);
expectType<number>()
  .assertBase(2 as const)
  .toBe(SuperType);
expectType(10).assert<boolean>().toBe(Unrelated);