1

Hi there I am new in typescript and i'd like to ask if it is possible get full name of class include module names

I have module like this

module System {
  export module Collections {
    export class MyClass{
    }
  }
}

than in another module i will create instance of MyClass

var inst = new System.Collections.MyClass();
inst.AssemblyQualifiedName()

and result will be System.Collections.MyClass

I need this for unique identifying instance/types in collection and for creating instance from string ...

Something like AssemblyQualifiedName in c#

Tân
  • 1
  • 15
  • 56
  • 102
Ivan Mjartan
  • 1,125
  • 1
  • 12
  • 23
  • I am able get class name GetType(): Type { return new Type(this.constructor.toString().match(/function (\w*)/)[1]); } like this but namespase ? – Ivan Mjartan Jan 27 '15 at 15:14

3 Answers3

4

You can use the TypeScript compiler API (requires Node.js), e.g.:

import ts = require("typescript");    

class Sample
    {
        private program: ts.Program = null;

        constructor(sourceFiles: string[])
        {
            var options: ts.CompilerOptions = { noLib: true };
            var host = ts.createCompilerHost(options)
            this.program = ts.createProgram(sourceFiles, options, host)
            this.program.getSourceFiles().forEach(file => this.doSomethingWithTheFile(file));
        }

        ...

        private getQualifiedName(node: ts.Node): string 
        {
            var symbol = this.program.getTypeChecker().getSymbolAtLocation(node);
            if (!symbol)
                return null;
            return this.program.getTypeChecker().getFullyQualifiedName(symbol);
        }
    }
jbenker
  • 206
  • 1
  • 6
2

No, it's not possible right out of the box. See this answer for more reference.

Possible Solutions

I'm not sure why you'd want or need to do this, but some possible solutions to your problem could be one of the following:

  1. Add a static property (ex. fullyQualifiedName) that contains a string with the fully qualified name in it. Write a unit test to run through your modules to ensure the name is correct on any class that has that static property.
  2. Add a static property again, but this time have your build script automatically fill in the name property.
  3. Have a method that runs at the start of your application that will fill in the static property of any classes in the module passed to it.

Example

An example of how to do this is the following:

module TestModule {
    export class TestClass1 {
        // initialize it to undefined or null to ensure the
        // typescript compiler adds it as a property
        static fullyQualifiedName: string = undefined as any;
    }

    export module OtherModule {
        export class TestClass2 {
            static fullyQualifiedName: string = undefined as any;
        }

        export class TestClass3 {
        }
    }
}

function fillFullyQualifiedName(m: any, name: string) {
    // if it's a module
    if (typeof m === "object") {
        for (const propName of Object.keys(m)) {            
            fillFullyQualifiedName(m[propName], name + "." + propName);
        }
    }
    // else, if it's a class with the fullyQualifiedName property
    else if (typeof m === "function" && m.hasOwnProperty("fullyQualifiedName")) {
        m["fullyQualifiedName"] = name;
    }
}

fillFullyQualifiedName(TestModule, "TestModule");

// some tests...
TestModule.TestClass1.fullyQualifiedName === "TestModule.TestClass1";
TestModule.OtherModule.TestClass2.fullyQualifiedName === "TestModule.OtherModule.TestClass2";
TestModule.OtherModule.TestClass3["fullyQualifiedName"] == null;

I didn't test that very much, but it should help you get the idea and apply it to any of the possible solutions I listed above.

David Sherret
  • 101,669
  • 28
  • 188
  • 178
1

There isn't a way to do this. Classes in JavaScript are just values, and there's no mechanism to say "where did I get this value from?"

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235