Skip to content
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

Introduce some metaprogramming types #4432

Merged
merged 8 commits into from
Apr 17, 2022
Merged

Conversation

inlined
Copy link
Member

@inlined inlined commented Apr 12, 2022

This blood is on your hands @taeold 😉

@inlined inlined requested a review from taeold April 12, 2022 18:57
Copy link
Contributor

@taeold taeold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR of the year? 🏆

@@ -1,18 +1,20 @@
import { SameType, LeafElems } from "./metaprogramming";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per linter: 'SameType' is defined but never used

: T[Key] extends object
? `${Key}` | `${Key}.${RecursiveKeyOf<T[Key]>}`
: `${Key}`;
}[keyof T & (string | number)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol I like how you broke syntax highlighting in github UI:

image

[Key in keyof T & (string | number)]: T[Key] extends unknown[]
? `${Key}`
: T[Key] extends object
? `${Key}` | `${Key}.${RecursiveKeyOf<T[Key]>}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern - latest version of typescript does not like this:

Type instantiation is excessively deep and possibly infinite.(2589)

playground

Try running it on lower version - no problems there!

Here's a response from the TS team about this regression:

microsoft/TypeScript#48552 (comment)

In fact, the example they are using (DeepKeys) looks identical (except that it also support union of objects too?)

Comment on lines +55 to +58
type RecursiveElems<T extends unknown[]> = {
[Key in keyof T]: T[Key] extends unknown[] ? T[Key] | RecursiveElems<T[Key]> : T[Key];
}[number];

Copy link
Contributor

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.

* 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woo such usefulness.

@inlined inlined enabled auto-merge (squash) April 17, 2022 01:29
@inlined inlined merged commit d3f860f into master Apr 17, 2022
@inlined inlined deleted the inlined.metaprogramming branch April 18, 2022 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants