How to use static class variable as initialization value for static class variable of other class with AMD in TypeScript?

0

please consider the following code, consisting of 3 simple classes, where I try to use a static class variable of a "common" class to initialize a static class variable of two other classes (to make sure they both use the same value). I'm using AMD in my project settings as module system.

Here is the first class:

import TestClassCommon from "TestClassCommon";

class TestClass1
{
    static ParameterNames =
    {
        CommonName: TestClassCommon.ParameterNames.CommonName,
        TestClass1Name: "TestClass1Name"
    }

    SomeAction()
    {
        console.log("TestClass1 action");
    }
}

export default TestClass1;

Here the second one:

import TestClassCommon from "TestClassCommon";

class TestClass2
{
    static ParameterNames =
    {
        CommonName: TestClassCommon.ParameterNames.CommonName,
        TestClass2Name: "TestClass2Name"
    }

    SomeAction()
    {
        console.log("TestClass2 action");
    }
}

export default TestClass2;

And the "common" one:

import TestClass1 from "TestClass1";
import TestClass2 from "TestClass2";

class TestClassCommon
{
    static ParameterNames =
    {
        CommonName: "CommonName"
    }

    static DoSomething()
    {
        console.log("Common name =     " + this.ParameterNames.CommonName);
        console.log("TestClass1 name = " + TestClass1.ParameterNames.TestClass1Name);
        console.log("TestClass2 name = " + TestClass2.ParameterNames.TestClass2Name);
    }
}

export default TestClassCommon;

When I then use the 2 classes like this:

import TestClass1 from "TestClass1";
import TestClass2 from "TestClass2";

let tc1 = new TestClass1();
let tc2 = new TestClass2();

I get an exception in TestClass2:

Unhandled exception at line 11, column 5 in http://localhost:54375/scripts/TestClass2.js

0x800a138f - JavaScript runtime error: Unable to get property 'ParameterNames' of undefined or null reference

I ran into a similar issue some time ago, but after some advice from other people switched to using the above way of importing, defining the classes and export the classes, and that solved the issues.

But now I ran into this issue again. Can anyone get this simple example to work? Thanks!

typescript
amd
asked on Stack Overflow Feb 22, 2017 by KoenT_X

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0