Giter Site home page Giter Site logo

derive's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.