0

Given the following code:

class Foo
{
    Bar(name: string): void
    {
        var x = name;
    }

    FooBar(): void
    {
    }
}

Using the typescript.js or something, is it possible to list all methods of the class Foo? How?

If possible I would like to do that without having to compile and run the Javascript. If there is no way I will try my luck with some regex...

3
  • I had a version of typescript playground for 0.8 : basarat.github.io/TypeScriptEditor you can use the source : github.com/basarat/TypeScriptEditor Commented Jul 9, 2013 at 22:14
  • @BASarat Can you be more specific? Which file do what I am trying to do? Commented Jul 9, 2013 at 22:52
  • Remember that TypeScript is a superset of JavaScript, so this will be extremely difficult (for example: look at how difficult it is for Google to do this in the Closure Compiler). However, if you just want to reflect on TypeScript classes, the you may hack the TypeScript compiler, use to TS parser to collect all the places where your class is defined, and then merge the methods list. Arguably it is easier than pure JavaScript, but it won't find places where you added new methods to the classes in JavaScript way. Commented Jul 11, 2013 at 8:09

1 Answer 1

1

You don't need typescript.js or anything similar, just use getOwnPropertyNames such as

var classMemberNames = Object.getOwnPropertyNames(Foo.prototype);

Methods of a TypeScript class appear on the prototype property of the constructor function representing the class.

Sign up to request clarification or add additional context in comments.

4 Comments

That requires compiling and running... I would really like to avoid that.
@BrunoLM You would normally compile + run to do reflection. Could you give an example in another language (C#, Java) of what you're trying to do?
@zcrar70 What I need is to parse the language... I need to know the methods, I would like to know them without having to compile and run. If there is no way I will resort to regex.
@zcrar70 My final goal is this: i.imgur.com/Qtgh2fX.png I ended up making dummy calls and using Regex to discover the tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.