-
Notifications
You must be signed in to change notification settings - Fork 1k
Introduce some metaprogramming types #4432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ee120b
Introduce some metaprogramming types
inlined c8dd42c
Fix breaks
inlined dc4be52
Merge branch 'master' into inlined.metaprogramming
inlined 89af92c
PR feedback
inlined 6922d9a
Merge remote-tracking branch 'origin/inlined.metaprogramming' into in…
inlined 7a8a475
Merge branch 'master' into inlined.metaprogramming
inlined a6bfec7
Merge branch 'master' into inlined.metaprogramming
inlined d53572b
Merge branch 'master' into inlined.metaprogramming
inlined File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
type Primitive = string | number | boolean | Function; | ||
|
||
/** | ||
* RecursiveKeyOf is a type for keys of an objet usind dots for subfields. | ||
* For a given object: {a: {b: {c: number}}, d } the RecursiveKeysOf are | ||
* 'a' | 'a.b' | 'a.b.c' | 'd' | ||
*/ | ||
export type RecursiveKeyOf<T> = T extends Primitive | ||
? never | ||
: | ||
| (keyof T & string) | ||
| { | ||
[P in keyof T & string]: RecursiveSubKeys<T, P>; | ||
}[keyof T & string]; | ||
|
||
type RecursiveSubKeys<T, P extends keyof T & string> = T[P] extends (infer Elem)[] | ||
? `${P}.${RecursiveKeyOf<Elem>}` | ||
: T[P] extends object | ||
? `${P}.${RecursiveKeyOf<T[P]>}` | ||
: never; | ||
|
||
/** | ||
* LeafKeysOf is like RecursiveKeysOf but omits the keys for any object. | ||
* For a given object: {a: {b: {c: number}}, d } the LeafKeysOf are | ||
* 'a.b.c' | 'd' | ||
*/ | ||
export type LeafKeysOf<T extends object> = { | ||
[Key in keyof T & (string | number)]: T[Key] extends unknown[] | ||
? `${Key}` | ||
: T[Key] extends object | ||
? `${Key}.${RecursiveKeyOf<T[Key]>}` | ||
: `${Key}`; | ||
}[keyof T & (string | number)]; | ||
|
||
/** | ||
* SameType is used in testing to verify that two types are the same. | ||
* Usage: | ||
* const test: SameType<A, B> = true. | ||
* The assigment will fail if the types are different. | ||
*/ | ||
export type SameType<T, V> = T extends V ? (V extends T ? true : false) : false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. woo such usefulness. |
||
|
||
type HeadOf<T extends string> = [T extends `${infer Head}.${infer Tail}` ? Head : T][number]; | ||
|
||
type TailsOf<T extends string, Head extends string> = [ | ||
T extends `${Head}.${infer Tail}` ? Tail : never | ||
][number]; | ||
|
||
/** | ||
* DeepOmit allows you to omit fields from a nested structure using recursive keys. | ||
*/ | ||
export type DeepOmit<T extends object, Keys extends RecursiveKeyOf<T>> = DeepOmitUnsafe<T, Keys>; | ||
|
||
type DeepOmitUnsafe<T, Keys extends string> = { | ||
[Key in Exclude<keyof T, Keys>]: Key extends Keys | ||
? T[Key] | undefined | ||
: Key extends HeadOf<Keys> | ||
? DeepOmitUnsafe<T[Key], TailsOf<Keys, Key>> | ||
: T[Key]; | ||
}; | ||
|
||
export type DeepPick<T extends object, Keys extends RecursiveKeyOf<T>> = DeepPickUnsafe<T, Keys>; | ||
|
||
type DeepPickUnsafe<T, Keys extends string> = { | ||
[Key in Extract<keyof T, HeadOf<Keys>>]: Key extends Keys | ||
? T[Key] | ||
: DeepPickUnsafe<T[Key], TailsOf<Keys, Key>>; | ||
}; | ||
|
||
/** In the array LeafElems<[[["a"], "b"], ["c"]]> is "a" | "b" | "c" */ | ||
export type LeafElems<T> = T extends Array<infer Elem> | ||
? Elem extends unknown[] | ||
? LeafElems<Elem> | ||
: Elem | ||
: T; | ||
|
||
/** | ||
* In the object {a: number, b: { c: string } }, | ||
* LeafValues is number | string | ||
*/ | ||
export type LeafValues<T extends object> = { | ||
[Key in keyof T]: T[Key] extends object ? LeafValues<T[Key]> : T[Key]; | ||
}[keyof T]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { expect } from "chai"; | ||
import { SameType, RecursiveKeyOf, LeafElems, DeepPick, DeepOmit } from "../metaprogramming"; | ||
|
||
describe("metaprogramming", () => { | ||
it("can calcluate recursive keys", () => { | ||
const test: SameType< | ||
RecursiveKeyOf<{ | ||
a: number; | ||
b: { | ||
c: boolean; | ||
d: { | ||
e: number; | ||
}; | ||
}; | ||
}>, | ||
"a" | "a.b" | "a.b.c" | "a.b.d" | "a.b.d.e" | ||
> = true; | ||
expect(test).to.be.true; | ||
}); | ||
|
||
it("can detect recursive elems", () => { | ||
const test: SameType<LeafElems<[[["a"], "b"], ["c"]]>, "a" | "b" | "c"> = true; | ||
expect(test).to.be.true; | ||
}); | ||
|
||
it("Can deep pick", () => { | ||
interface original { | ||
a: number; | ||
b: { | ||
c: boolean; | ||
d: { | ||
e: number; | ||
}; | ||
g: boolean; | ||
}; | ||
h: number; | ||
} | ||
|
||
interface expected { | ||
a: number; | ||
b: { | ||
c: boolean; | ||
}; | ||
} | ||
|
||
const test: SameType<DeepPick<original, "a" | "b.c">, expected> = true; | ||
expect(test).to.be.true; | ||
}); | ||
|
||
it("can deep omit", () => { | ||
interface original { | ||
a: number; | ||
b: { | ||
c: boolean; | ||
d: { | ||
e: number; | ||
}; | ||
g: boolean; | ||
}; | ||
h: number; | ||
} | ||
|
||
interface expected { | ||
b: { | ||
d: { | ||
e: number; | ||
}; | ||
g: boolean; | ||
}; | ||
h: number; | ||
} | ||
|
||
const test: SameType<DeepOmit<original, "a" | "b.c">, expected> = true; | ||
expect(test).to.be.true; | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think we don't need this anymore.