2023-01-22 01:34:57 +00:00
|
|
|
import type {Dependencies, Signature} from '../interfaces/type.js'
|
2023-08-20 17:28:39 +00:00
|
|
|
import {$$typeToString, $$ts, $$define, $$raw, $$ident, $$escape} from 'ts-macros'
|
|
|
|
import * as ts from 'typescript'
|
2023-01-22 01:34:57 +00:00
|
|
|
|
|
|
|
export const square =
|
|
|
|
<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
|
|
|
|
z => dep.multiply(z, z)
|
2023-08-18 17:36:11 +00:00
|
|
|
// z => dep.fooBar(z, z) // fails as desired
|
2023-08-18 17:57:30 +00:00
|
|
|
// z => dep.multiply(z, 'foo') // still fails as desired
|
|
|
|
|
|
|
|
// make sure ts-macros is running
|
|
|
|
function $contains<T>(value: T, ...possible: Array<T>) {
|
|
|
|
// repetition which goes over all the elements in the "possible" array
|
|
|
|
return +["||", [possible], (item: T) => value === item];
|
|
|
|
}
|
|
|
|
|
|
|
|
const searchItem = "needle";
|
|
|
|
console.log("In generic arithmetic")
|
|
|
|
console.log($contains!(searchItem, "erwin", "tj"));
|
|
|
|
// Transpiles to: console.log(false);
|
|
|
|
|
|
|
|
// OK, record the type of square:
|
2023-08-20 17:28:39 +00:00
|
|
|
square.reflectedType = //$$typeToString!<typeof square>();
|
|
|
|
$$typeToString!<
|
|
|
|
<T>(...args: DeepExpand<Parameters<typeof square<T>>>)
|
|
|
|
=> DeepExpand<ReturnType<typeof square<T>>>
|
|
|
|
>()
|
2023-08-18 22:00:21 +00:00
|
|
|
|
2023-08-19 03:29:24 +00:00
|
|
|
type ShallowExpand<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
|
2023-08-20 17:28:39 +00:00
|
|
|
// Short alias of ShallowExpand for convenience below:
|
|
|
|
type Deps<T> = T extends unknown ? { [K in keyof T]: T[K] } : never;
|
2023-08-19 03:29:24 +00:00
|
|
|
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> =
|
2023-08-20 17:28:39 +00:00
|
|
|
T extends (...args: any) => any
|
|
|
|
? (...pars: AsArray<DeepExpand<Parameters<T>>>) => DeepExpand<ReturnType<T>> :
|
2023-08-19 03:29:24 +00:00
|
|
|
T extends unknown ? { [K in keyof T]: DeepExpand<T[K]> } : never;
|
2023-08-20 17:28:39 +00:00
|
|
|
|
2023-08-19 03:29:24 +00:00
|
|
|
type Out<T> = ShallowExpand<Dependencies<'multiply', T>>;
|
2023-08-18 22:00:21 +00:00
|
|
|
|
2023-08-19 03:29:24 +00:00
|
|
|
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>>>
|
|
|
|
>())
|
2023-08-20 17:28:39 +00:00
|
|
|
|
|
|
|
// Now try to wrap it up in a macro
|
|
|
|
|
|
|
|
// From the creator of ts-macros; temporary until next release
|
|
|
|
function $export(name: string, value: unknown) {
|
|
|
|
$$raw!((ctx, name: ts.Expression, value: ts.Expression) => {
|
|
|
|
if (!ctx.ts.isStringLiteral(name)) throw ctx.error(name, "Expected a string literal.");
|
|
|
|
return [ctx.factory.createVariableStatement([ctx.factory.createToken(ctx.ts.SyntaxKind.ExportKeyword)], ctx.factory.createVariableDeclarationList([
|
|
|
|
ctx.factory.createVariableDeclaration(name.text, undefined, undefined, value)
|
|
|
|
], ctx.ts.NodeFlags.Const))];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obviously we'd prefer the name before the expression, but that won't work
|
|
|
|
// until the next release
|
|
|
|
function $exportImpl<Impl>(expr: Impl, name: string) {
|
|
|
|
$export!(name, expr);
|
|
|
|
$$ident!(name).reflectedType = $$typeToString!<
|
|
|
|
// <T>(...args: DeepExpand<Parameters<Impl<T>>>) => DeepExpand<ReturnType<Impl<T>>> // see comment in reflect below.
|
|
|
|
Impl
|
|
|
|
>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// works but then not visible at import with current ts-macros.
|
|
|
|
// Author says he will be enhancing this "soon."
|
|
|
|
$exportImpl!(<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
|
|
|
|
z => dep.multiply(z, z),
|
|
|
|
'squire')
|
|
|
|
|
|
|
|
function $reflect<Impl>(expr: Impl) {
|
|
|
|
return $$escape!(() => {
|
|
|
|
const temp: Impl & {reflectedType?: string} = expr;
|
|
|
|
temp.reflectedType = $$typeToString!<
|
|
|
|
// <T>(...args: DeepExpand<Parameters<Impl<T>>>) =>
|
|
|
|
// DeepExpand<ReturnType<Impl<T>>> // error; can't instantiate
|
|
|
|
// Impl; even if we constrain it to be the type of a generic function,
|
|
|
|
// that's still not a generic _type_, ugh.
|
|
|
|
Impl>();
|
|
|
|
return temp;
|
|
|
|
}) as Impl & {reflectedType: string}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const squre = $reflect!(
|
|
|
|
<T>(dep: Dependencies<'multiply', T>): Signature<'square', T> =>
|
|
|
|
z => dep.multiply(z, z))
|
|
|
|
|
|
|
|
export const sqre = $reflect!(
|
|
|
|
<T>(dep: Deps<Dependencies<'multiply', T>>): Signature<'square', T> =>
|
|
|
|
z => dep.multiply(z, z))
|