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

@shopify/useful-types

v5.3.0

Published

A few handy TypeScript types

Downloads

373,091

Readme

@shopify/useful-types

Build Status Build Status License: MIT npm version npm bundle size (minified + gzip)

A few handy TypeScript types.

Installation

yarn add @shopify/useful-types

Usage

The following type aliases are provided by this library:

  • ThenType<T>: When T is a promise, resolves to the type the promise will resolve to (Promise<infer U>). Otherwise, resolves to T. Useful when you may or may not have a promise and need to reference the underlying type.

    const value = 'foo';
    const promise = Promise.resolve(value);
    
    type V = ThenType<typeof value>; // string
    type P = ThenType<typeof promise>; // string
  • ArgumentAtIndex<T, Index>: Resolves to the type of the argument of the passed function at the passed index. Useful for cases where you wish to extract the type of arguments without actually exporting the argument types, and is a nice complement to TypeScript’s built-in ReturnType.

    const func = (foo: Promise<any>, bar: string) => foo.then(() => bar);
    type Arg1 = ArgumentAtIndex<typeof func, 0>; // Promise<any>
    type Arg2 = ArgumentAtIndex<typeof func, 1>; // string
    type NotAnArg = ArgumentAtIndex<string, 0>; // never
  • FirstArgument<T>: Resolves to the type of the first argument to the passed function. This is shorthand for ArgumentAtIndex<T, 0>.

    const func = (foo: Promise<any>) => foo.then(() => 'bar');
    type Arg = FirstArgument<typeof func>; // Promise<any>
  • ConstructorArgumentAtIndex<T, Index>: Resolves to the type of the argument of the passed class's constructor at the passed index. Useful for cases where you wish to extract the type of arguments without actually exporting the argument types, and is a nice complement to TypeScript’s built-in ReturnType.

    class SomeClass {
      constructor(floop: string, doop: number) {
        console.log(floop);
      }
    }
    
    type DoopType = ConstructorArgument<typeof SomeClass, 1>; // number
  • FirstConstructorArgument<T>: Resolves to the type of the first argument to the passed class's constructor. This is shorthand for ConstructorArgumentAtIndex<T, 0>.

    class SomeClass {
      constructor(floop: string) {
        console.log(floop);
      }
    }
    
    type DoopType = FirstConstructorArgument<typeof SomeClass>; // string
  • ArrayElement<T>: When T is an array, resolves to the type contained within the array.

    type FooArray = (string | number)[];
    type Foo = ArrayElement<FooArray>; // string | number
  • Omit<T, K extends keyof T>: The opposite of TypeScript’s Pick type. Resolves to a new type that includes all keys in the original except those matching K.

    interface Obj {
      foo: string;
      bar: boolean;
      baz: number;
    }
    
    type SelectiveObj = Omit<Obj, 'foo' | 'bar'>; // {baz: number}
  • DeepPartial<T>: Recursively maps over all properties in a type and transforms them to be optional. Useful when you need to make optional all of the properties (and nested properties) of an existing type.

    interface Obj {
      foo: string;
      bar: {
        baz: boolean;
      };
    }
    
    type DeepPartialObj = DeepPartial<Obj>; // {foo?: string; bar?: { baz?: boolean }}
  • NoInfer<T>: creates a "lower priority inference site", which allows other uses of a generic to take precedence in inference.

    // Here, TypeScript will always use the type of the items in the `items` argument as `T`, and will not consider the type of the `item` argument of `renderItem`.
    function render<T>(items: T[], renderItem: (item: NoInfer<T>) => string) {
      /* implementation */
    }
  • DeepOmit<T, K> Recursively maps over all properties in a type and omits those matching K.

interface Obj {
  __typename: string;
  foo: string;
  bar: {
    __typename: string;
    baz: string;
  };
}

type SelectiveObj = DeepOmit<Obj, '__typename'>; // {foo: string; bar: {baz: string}}
  • DeepOmitArray<T extends any[], K> Iterate over all properties in an array of types and omits those matching K.

    interface Obj {
      __typename: string;
      foo: string;
    }
    
    type SelectiveObj = DeepOmitArray<Obj[], '__typename'>; // {foo: string}[]
  • PartialSome<T, K extends keyof T> Make specified keys K of T optional.

    interface Obj {
      foo: string;
      bar: string;
    }
    
    type HalfPartialObj = PartialSome<Obj, 'foo'>; // {foo?: string, bar: string}
  • RequireSome<T, K extends keyof T> Make specified keys K of T required.

    interface Obj {
      foo?: string;
      bar?: string;
    }
    
    type HalfRequiredObj = RequireSome<Obj, 'foo'>; // {foo: string, bar?: string}
  • DeepReadonly<T>: Recursively maps over all properties in a type and transforms them to be read-only.

    interface Obj {
      foo: string;
      bar: {
        baz: boolean;
      };
    }
    
    type DeepReadonlyObj = DeepReadonly<Obj>; // {readonly foo: string; readonly bar: { readonly baz: boolean }}