-
Notifications
You must be signed in to change notification settings - Fork 969
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
Conversation
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.
PR of the year? 🏆
src/functional.ts
Outdated
@@ -1,18 +1,20 @@ | |||
import { SameType, LeafElems } from "./metaprogramming"; |
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.
per linter: 'SameType' is defined but never used
src/metaprogramming.ts
Outdated
: T[Key] extends object | ||
? `${Key}` | `${Key}.${RecursiveKeyOf<T[Key]>}` | ||
: `${Key}`; | ||
}[keyof T & (string | number)]; |
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.
src/metaprogramming.ts
Outdated
[Key in keyof T & (string | number)]: T[Key] extends unknown[] | ||
? `${Key}` | ||
: T[Key] extends object | ||
? `${Key}` | `${Key}.${RecursiveKeyOf<T[Key]>}` |
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.
One concern - latest version of typescript does not like this:
Type instantiation is excessively deep and possibly infinite.(2589)
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?)
type RecursiveElems<T extends unknown[]> = { | ||
[Key in keyof T]: T[Key] extends unknown[] ? T[Key] | RecursiveElems<T[Key]> : T[Key]; | ||
}[number]; | ||
|
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.
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
woo such usefulness.
This blood is on your hands @taeold 😉