TypeScript utility generating new types by deep picking/omitting, leveraging Template Literal Types from TypeScript >= 4.1
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.
``ts
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
// Result type: { foo: { a: number, b: number } };
type FooWithJustAB = DeepPick
// Result type: { foo: { a: number, b: number } };
type FooWithoutC = DeepPick
// Result type: { bar: ABC };
type Bar = DeepPick
// 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
// Result type: Array<{ foo: ABC }>
type ArrayWithFoo = DeepPick
// Result type: FooBar
type ArrayWithFoo = DeepPick
`
Returns the possible path that can be used to passed into DeepPick for a given
structure.
`ts
import { DeepPickPath } from "ts-deep-pick";
// assume FooBar as defined earlier.
// "foo" | "bar" | "!foo" | "!bar" | "~foo.a" | "~foo.!a" | ...
type Path = DeepPickPath
`
- Union paths are treated like a combination of paths. For example, "a"|"b"a
would mean "pick properties and b"."a"
- 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 () then these keys and the mutations ("~a") become the picked"!a"
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 () and"~a"
mutations (), then the omit operation takes place on the full set of
keys.
- "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 thea
only property of ."!a" | "~b.foo"
- will pick everything except for a at the outermost level,b
and modifies such that only foo is available there.
The character tokens ., !, ~, [] and * define the propertyDeepPick
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 and DeepPickPath.
`ts`
interface DeepPickGrammar {
array: string; // default: "[]"
prop: string; // default: "."
omit: string; // default: "!"
mutate: string; // default: "~"
glob: string; // default: "*"
}
Example:
`ts
import { DefaultGrammar } from "ts-deep-pick";
interface G extends DeepPickGrammar {
props: ":"; // Now use ":" as property separator
}
// Result type: { foo: { a: number } };
type FooWithJustA = DeepPick
`
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.
`ts
// 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
``