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

Question: Is it possible to do a "type-safe" repetition? #78

Closed
gwhitney opened this issue Sep 30, 2023 · 2 comments
Closed

Question: Is it possible to do a "type-safe" repetition? #78

gwhitney opened this issue Sep 30, 2023 · 2 comments

Comments

@gwhitney
Copy link

gwhitney commented Sep 30, 2023

Is your feature request related to a problem? Please describe.
Suppose I make a macro:

function $reflect<T>(expr: T) {
   expr.reflectedType = $$typeToString!<T>(true, false, true);
}

Then indeed the following code

export const foo = {x: 1, y: 2}
export const bar = {a: 'A', b: 'B'}
export const baz = {yes: true, no: false}
$reflect!(foo)
$reflect!(bar)
$reflect!(baz)

generates

export const foo = {x: 1, y: 2}
export const bar = {a: 'A', b: 'B'}
export const baz = {yes: true, no: false}
foo.reflectedType = "{x: number, y: number}";
bar.reflectedType = "{a: string, b.string}";
baz.reflectedType = "{yes: boolean, no: boolean}";

as desired (ignore for the moment that it then generates a TypeScript error since the type of foo has no reflectedType property).

But what If I would like to create a different macro so that I could abbreviate the last three lines with say:

$reflectTypes!([foo, bar, baz])

and generate the exact same code?

Describe the solution you'd like
I'd like there to be a way to cycle through an array of literals, accessing the type of each in turn.

Describe alternatives you've considered
I tried

export function $reflectTypes<ImplTuple>(tup: ImplTuple) {
   +[[tup], <T>(elt: T) =>
      elt.reflectedType = $$typeToString!<T>(true, false, true)]
}

but that fails with

TS8000: `typeToString` macro expects one type parameter.

even though it certainly looks like I gave it one type parameter. I also tried the more baroque

export function $reflectTypes<ImplTuple>(tup: ImplTuple) {
   +[[tup, [0,1,2]], <N extends number>(elt: ImplTuple[N], n: N) =>
      elt.reflectedType = $$typeToString!<ImplTuple[N]>(true, false, true)]
}

and that actually seems to get through macro processing but it produces "{}" for all of the type strings.
Maybe I am just missing how this should be done? If so, guidance would be welcome. Thanks!

Additional context
Currently running ts-macros 2.5.0, ts-patch 3.0.2

GoogleFeud added a commit that referenced this issue Oct 1, 2023
@GoogleFeud
Copy link
Owner

Hi, I've never thought of adding this feature, but it will be added in the next version!

@gwhitney
Copy link
Author

Works for me in ts-macros 2.6.0, thanks!

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

No branches or pull requests

2 participants