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-deep-pick

v0.2.2

Published

TypeScript utility generating new types by deep picking/omitting, leveraging Template Literal Types from TypeScript >= 4.1

Downloads

27,223

Readme

ts-deep-pick is a TypeScript type-only utility package that allows for type-safe, deep picking/omitting of properties from nested object types. Powered by the template literal type released in TypeScript 4.1, the type DeepPick will offer all combinations of nested pick/omit string patterns that will then return the deeply picked/omit type according to the deep-picking grammar.

Screenshot

Usage

DeepPick

import { DeepPick } from "ts-deep-pick";

interface FooBar {
  foo: ABC;
  bar: ABC;
}

interface ABC {
  a: number;
  b: number;
  c: number;
}

// Result type: { foo: { a: number } };
// including a property "positively" omits the exluded properties.
type FooWithJustA = DeepPick<FooBar, "foo.a">; // "." joins nested properties

// Result type: { foo: { a: number, b: number } };
type FooWithJustAB = DeepPick<FooBar, "foo.a" | "foo.b">;properties

// Result type: { foo: { a: number, b: number } };
type FooWithoutC = DeepPick<FooBar, "foo.!c">; // "!" omits a property

// Result type: { bar: ABC };
type Bar = DeepPick<FooBar, "!bar">; // "!" omits a property

// Result type:
// {
//    foo: { a: number, b: number },
//    bar: ABC
// };
// "~" mutates the property's nested membership without explicitly picking
// the property and thereby omitting the remaining.
type FooBarWithoutFooC = DeepPick<FooBar, "~foo.!c">;

// Result type: Array<{ foo: ABC }>
type ArrayWithFoo = DeepPick<FooBar[], "[].foo"> // "[]" accesses an array item.

// Result type: FooBar
type ArrayWithFoo = DeepPick<FooBar, "*"> // "*" is the "identity" operand

DeepPickPath

Returns the possible path that can be used to passed into DeepPick for a given structure.

import { DeepPickPath } from "ts-deep-pick";

// assume FooBar as defined earlier.
// "foo" | "bar" | "!foo" | "!bar" | "~foo.a" | "~foo.!a" | ...
type Path = DeepPickPath<FooBar>;

Behavior

  • Union paths are treated like a combination of paths. For example, "a"|"b" would mean "pick properties a and b".
  • At each level, the "picked set" is determined before omitting and mutating. If the paths at the given level include at least one explicitly picked key ("a") then these keys and the mutations ("~a") become the picked set. In this case, omits are redundant because they are either no-op (omitting keys that aren't picked) or they make the picks no-op (omitting keys that are picked).
  • If the paths at the given level consist only of omits ("!a") and mutations ("~a"), then the omit operation takes place on the full set of keys.

Examples

  • "a" | "b" will pick just properties a and b.
  • "a" | "!a" will pick nothing (a is picked but is then omitted.)
  • "~a.foo" will pick everything at the outermost level, and pick foo as the only property of a.
  • "!a" | "~b.foo" will pick everything except for a at the outermost level, and modifies b such that only foo is available there.

Grammar

The character tokens ., !, ~, [] and * define the property separator, omit prefix, mutation prefix, array index property and the pass-through (return the original type) operand respectively. These tokens can be replaced by defining the following interface and passing it as the extra type parameter to DeepPick and DeepPickPath.

interface DeepPickGrammar {
  array: string;  // default: "[]"
  prop: string;   // default: "."
  omit: string;   // default: "!"
  mutate: string; // default: "~"
  glob: string;   // default: "*"
}

Example:

import { DefaultGrammar } from "ts-deep-pick";

interface G extends DeepPickGrammar {
  props: ":"; // Now use ":" as property separator
}

// Result type: { foo: { a: number } };
type FooWithJustA = DeepPick<FooBar, "foo:a", G>;

AmbiguousProp<G = DefaultGrammar>

Returns the template literal type that represents any string that -- if used as a property name -- would be ambigous given the set of character tokens.

// Result type: "a"
// ("!a" is ambiguous because of the "!" character).
// ("a.b" is ambiguous because of the "." character)
type ValidProps = Exclude<"a" | "!a" | "a.b", AmbiguousProps>

// Result type: "a" | "a.b"
// ("a:b" would have been ambiguous now because of custom grammar G defined
// earlier).
type ValidProps = Exclude<"a" | "!a" | "a.b", AmbiguousProps<G>>