Skip to content

shoooe/derive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

6de679d Β· Mar 8, 2023

History

37 Commits
Mar 8, 2023
Nov 17, 2022
Mar 8, 2023
Mar 8, 2023
Mar 8, 2023
Mar 8, 2023
Oct 18, 2022
Oct 17, 2022
Oct 17, 2022
Mar 8, 2023
Mar 8, 2023
Mar 8, 2023
Mar 8, 2023
Nov 17, 2022
Mar 8, 2023
Nov 17, 2022

Repository files navigation

Derive

npm version

Utility type to generate a type from another with a special care for the DX.

You can see this tool as an hardcode version of Pick.

Features:

  • 😎 Type safe
  • 🌱 Minimal & lightweight
  • βŒ¨οΈβ€‹ Autocompletion for fields
  • πŸ’₯ Automatically expands arrays and nullable/optional types
  • πŸ‘€ Preview expanded types in intellisense
  • πŸ’« Supports recursive & mutually recursive types
  • ❓ Optional fields support
  • πŸ’‹ Inspired by GraphQL

Installation

yarn add @shoooe/derive

Usage

Basic usage

Let's say that we have a type User defined as:

type User = {
  bestFriend: User | undefined;
  favoriteBook: Book | null;
  friends: User[] | undefined;
  id: number;
  name: string;
};

type Book = {
  author: User;
  isdn: number;
  synopsis: string | null;
  title: string | null | undefined;
  subtitle?: string | null;
};

We can derive a subset of its properties via:

type Result = Derive<
  User,
  {
    id: true;
    name: true;

    // Automatically expands nullable & optional types, which means that `null`
    // and `undefined` will be added automatically to the resulting type if
    // they existed in the target type.
    bestFriend: {
      name: true;
    };

    // Automatically expands arrays as well
    friends: {
      name: true;

      // Supports mutually recursive types
      favoriteBook: {
        isdn: true;
        title: true;
        synopsis: true;
        author: {
          name: true;
        };
      };
    };
  }
>;

Which will result in:

type Result = {
  id: number;
  name: string;
  bestFriend:
    | {
        name: string;
      }
    | undefined;
  friends:
    | {
        name: string;
        favoriteBook: {
          isdn: number;
          title: string | null | undefined;
          synopsis: string | null;
          author: {
            name: string;
          };
        } | null;
      }[]
    | undefined;
};

Alternatives

  • ts-essentials: comprehensive library with a different style for DeepPick (it doesn't do automatic expansion)

Credits

Special thanks to:

  • Perdoo for sponsoring the initial research & implementation
  • Szaman for the initial code review

Packages

No packages published