@shopify/useful-types
A few handy TypeScript types
Last updated 24 days ago by shopify-dep .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @shopify/useful-types 
SYNC missed versions from official npm registry.

@shopify/useful-types

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>
    
  • 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>: Recusively 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 */
    }
    

Current Tags

  • 2.1.2                                ...           latest (24 days ago)

16 Versions

  • 2.1.2                                ...           24 days ago
  • 2.1.1                                ...           a month ago
  • 2.1.0                                ...           2 months ago
  • 2.0.2                                ...           2 months ago
  • 2.0.1                                ...           3 months ago
  • 2.0.0                                ...           4 months ago
  • 1.3.0                                ...           7 months ago
  • 1.2.4                                ...           8 months ago
  • 1.2.3                                ...           8 months ago
  • 1.2.2                                ...           8 months ago
  • 1.2.1                                ...           8 months ago
  • 1.2.0                                ...           9 months ago
  • 1.1.2                                ...           10 months ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.0                                ...           a year ago

Copyright 2014 - 2016 © taobao.org |