feat: Runtime type reflection #17

Merged
glen merged 21 commits from ts-macros-issues into main 2023-10-17 22:02:18 +00:00
Showing only changes of commit af02f1cb29 - Show all commits

View File

@ -21,7 +21,21 @@ console.log($contains!(searchItem, "erwin", "tj"));
// OK, record the type of square:
square.reflectedType = $$typeToString!<typeof square>();
type Expand<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
type Out = Expand<Dependencies<'multiply', number>>;
type ShallowExpand<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
type ExpandedParameters<T extends (...args: any) => any> = {[Index in keyof Parameters<T>]: ShallowExpand<Parameters<T>[Index]>}
type ExpandTwice<T extends any[]> = {[Index in keyof T] : ShallowExpand<T[Index]>}
type AsArray<T> = T extends any[] ? T : [];
type DeepExpand<T> =
T extends (...args: any) => any ? (...pars: AsArray<DeepExpand<Parameters<T>>>) => DeepExpand<ReturnType<T>> :
T extends unknown ? { [K in keyof T]: DeepExpand<T[K]> } : never;
type Out<T> = ShallowExpand<Dependencies<'multiply', T>>;
console.log("Deps type is", $$typeToString!<Out>())
console.log("Deps type is", $$typeToString!<<T>() => Out<T>>())
type OutB = ExpandTwice<Parameters<typeof square>>;
console.log("Or perhaps", $$typeToString!<OutB>())
console.log("Or maybe even", $$typeToString!<
<T>(...args: DeepExpand<Parameters<typeof square<T>>>) => DeepExpand<ReturnType<typeof square<T>>>
>())