ts-macros icon indicating copy to clipboard operation
ts-macros copied to clipboard

[BUG] Calling static method on a class from identifier crashing playground

Open TheMrAmazing opened this issue 2 years ago • 3 comments

The macro here seems to crash the playground site, and is not working on my project: https://googlefeud.github.io/ts-macros/playground?code=FAMwrgdgxgLglgewgAgCQjhAJgIQJ4CSWAPACoB8AFAJTIDewwyzaUATgKYCGMHAYpix9IsRBACEZKqlQw8ABw6kEAZRhtMAc0kUaAGhlwsHCDHGUA5BmwXkAahlzFytRojapNe8gv4iF6mpgAF9GcGh4JFZOHn5BYQixTycOADkuAFsOAC5kAGd1LT0AOlKITJzkAEE2Ni48YgK3TXJaBhZ7AG0vAF5yeiYWLjy8aGRw0SjyrORKTgBHXIAlDnmwDgK9ZE485Y35JDyOLYgOAA8YXKatNsGO5nU8AfuX+6hDhAAbDmLPhE1KIZjKZzCl0llqMVwkFXshgsgoDwoAALWYcWoINi3WEdAD0uNOF0o6LYmJhr1CHVCwQAuiFgFBPsM8sgAMLIrgQU6fZ4sAo8OBQcaQbLXdzIHo+XgFCz04DoQR+LCSdmc7lUILAfEAARgeQAtHBNBBMRxQIJVVyOJ8lZYAIwWLYWABMjp8AGYAkA

I am trying to call the static method on a class, but using $$ident! for a class to call that static method seems to not be working. Also, it would be really cool if you could generate a function that doesn't need //@ts-ignore when you call it

TheMrAmazing avatar Jun 11 '22 14:06 TheMrAmazing

Thanks for the bug report! It seems like the playground doesn't support promises, but if you remove the async keyword from the function it seems to run correctly. I'll fix this in the future!

https://googlefeud.github.io/ts-macros/playground?code=FAMwrgdgxgLglgewgAgCQjhAJgIQJ4CSWAPACoB8AFAJTIDewwyzaUATgKYCGMHAYpix9IsRBACEZKqlQw8ABw6kEAZRhtMAc0kUaAGhlwsHCDHGUA5BmwXkAahlzFytRojapNe8gv4iF6mpgAF9GcGh4JFZOHn5BYQixTycOADkuAFsOAC5kAGd1LT0AOlKITJzkAEE2Ni48YgK3TXJaBhZ7AG0vAF5yeiYWcNEo8qzkSk4AR1yAJQ4psA4CvWROPLnl+SQ8jlWIDgAPGFymrTbBjuZ1PAGr+6uoHYQAGw5il4RNSkNjU3MUukstRiuEgg9kMFkFAeFAABYTDi1BBsC4QjoAegxB2OlCRbBR4IeoQ6oWCAF0QsAoC8uHk8sgAMJwrgQA4vO4sAo8OBQZDhbJndzIHo+XgFCxU4DoQR+LCSZms9lUILS1BGExmSgAImsWEVbI4Lzl2uoOoAjNrVtqAExW5DagDMpoA3EA

And as you can see in the example, you can call any function with the $$ident keyword! It's definitely not as pretty as a simple function call, but sadly this plugin cannot alter any typescript errors, because it all happens after type checking: $$ident!("findChannelById")("1", "2", "3");

As for your second question, can you provide an example? This seems to be transpiling correctly: console.log($$ident!(typeName).fun()) -> console.log(Channel.fun());, unless you mean something else?

GoogleFeud avatar Jun 11 '22 18:06 GoogleFeud

Thanks a lot! Awesome work on this project, I'm having a lot of fun with it, and it definitely fills a hole in the Typescript ecosystem. I think the issue I was running into was related to #8 because in my real project, Channel was defined in a different file.

TheMrAmazing avatar Jun 11 '22 18:06 TheMrAmazing

Thank you so much, I'm glad you're enjoying this plugin! Can you share the code which causes the issue? I'm currently trying to replicate it like so:

channel.ts

export class Channel {
    static fun:string = 'test'
}

index.ts

import { Channel } from "./vector";
import { $$typeToString, $$ident } from "ts-macros";

function $findById<T>() {

    $createFindFunction!<T>($$typeToString!<T>(),$$ident!(`find${$$typeToString!<T>()}ById`))
}

function $createFindFunction<T>(typeName: string,...name: Array<string>) {
    +[() => {
    function name (req: Request, res: Response, next: string) {
        try {
                console.log($$ident!(typeName).fun())
        } catch (error) {
            //next(error)
        }
    }
}]
}

$findById!<Channel>();

$$ident!("findChannelById")("1", "2", "3");

But everything works as expected!

GoogleFeud avatar Jun 11 '22 19:06 GoogleFeud