Overwrite / extend typescript class expressions











up vote
0
down vote

favorite
1












My company has developed a own test framework for the software, because it's not possible to use an open-source framework in the context of the software we develop.



Anyway, I wrote Typescript Definition Files to improve the code completion for VS Code, but I can't make it work for the following setup:



The following class is used to define unit tests. It provides some functions, e.g. for assertions. The main problem is the util-namespace. You can pass a function which is actually the unit test. Note that the this-object of that function points to the unit test class so that the passed function can make use of the functions of the unit test (internally the passed function will be called with "unitTest.call(this)" to set the this object of the function to the unit test instance)



class UnitTest {
constructor(unitTest: (this: UnitTest) => void);
.
.
.
util = Util; // Referes to the Util-Class (it's a class because its the only possibility in typescript for nesting


The Util-class provides some general functions which are useful for writing tests



class Util {
static createFile(filePath: string): File;
.
.
.
}


If you declare a Unit Test like this, everything works fine:



var unitTest = new UnitTest(function() {
this.util.createFile("..."); // VS-Code provides code completion for that functions of the unit-test-namespace
});


The problem is, that the util-class (which is used for
code encapsulation) can be extended in every project. You can define a script following a specific name schema and the testframework dynamically loads that script and provides that functions in the util namespace, e.g. "test.util.myProject.js"



module.exports = {
myCustomUtilFunction = function () { ... }
};


Just by providing the script you can use that function in your unit test:



var unitTest = new UnitTest(function() {
this.util.myCustomUtilFunction();
});


But I can't cover this with the typescript definition file. VS-Code will not provide any code completion for the custom util functions because it's missing in the typescript definition of the test framework. I tried to extend the Util-class as follow, but VS-Code doesn't care:



class Util {
static myCustomUtilFunction(): void;
}


Any idea how to solve this?





To make it easier to understand, here is the complete setup
testframework.d.ts



class UnitTest {
constructor(unitTest: (this: UnitTest) =>

util = Util;
}

class Util {
static createFile(filePath: string): File;
}


myExtendedUtilNamespace.d.ts



class Util {
static myCustomUtilFunction(): void;
}


unitTests.js



var unitTest = new UnitTest(function() {
this.util.createFile("..."); // works
this.util.myCustomUtilFunction(); // does not work
});









share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    My company has developed a own test framework for the software, because it's not possible to use an open-source framework in the context of the software we develop.



    Anyway, I wrote Typescript Definition Files to improve the code completion for VS Code, but I can't make it work for the following setup:



    The following class is used to define unit tests. It provides some functions, e.g. for assertions. The main problem is the util-namespace. You can pass a function which is actually the unit test. Note that the this-object of that function points to the unit test class so that the passed function can make use of the functions of the unit test (internally the passed function will be called with "unitTest.call(this)" to set the this object of the function to the unit test instance)



    class UnitTest {
    constructor(unitTest: (this: UnitTest) => void);
    .
    .
    .
    util = Util; // Referes to the Util-Class (it's a class because its the only possibility in typescript for nesting


    The Util-class provides some general functions which are useful for writing tests



    class Util {
    static createFile(filePath: string): File;
    .
    .
    .
    }


    If you declare a Unit Test like this, everything works fine:



    var unitTest = new UnitTest(function() {
    this.util.createFile("..."); // VS-Code provides code completion for that functions of the unit-test-namespace
    });


    The problem is, that the util-class (which is used for
    code encapsulation) can be extended in every project. You can define a script following a specific name schema and the testframework dynamically loads that script and provides that functions in the util namespace, e.g. "test.util.myProject.js"



    module.exports = {
    myCustomUtilFunction = function () { ... }
    };


    Just by providing the script you can use that function in your unit test:



    var unitTest = new UnitTest(function() {
    this.util.myCustomUtilFunction();
    });


    But I can't cover this with the typescript definition file. VS-Code will not provide any code completion for the custom util functions because it's missing in the typescript definition of the test framework. I tried to extend the Util-class as follow, but VS-Code doesn't care:



    class Util {
    static myCustomUtilFunction(): void;
    }


    Any idea how to solve this?





    To make it easier to understand, here is the complete setup
    testframework.d.ts



    class UnitTest {
    constructor(unitTest: (this: UnitTest) =>

    util = Util;
    }

    class Util {
    static createFile(filePath: string): File;
    }


    myExtendedUtilNamespace.d.ts



    class Util {
    static myCustomUtilFunction(): void;
    }


    unitTests.js



    var unitTest = new UnitTest(function() {
    this.util.createFile("..."); // works
    this.util.myCustomUtilFunction(); // does not work
    });









    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      My company has developed a own test framework for the software, because it's not possible to use an open-source framework in the context of the software we develop.



      Anyway, I wrote Typescript Definition Files to improve the code completion for VS Code, but I can't make it work for the following setup:



      The following class is used to define unit tests. It provides some functions, e.g. for assertions. The main problem is the util-namespace. You can pass a function which is actually the unit test. Note that the this-object of that function points to the unit test class so that the passed function can make use of the functions of the unit test (internally the passed function will be called with "unitTest.call(this)" to set the this object of the function to the unit test instance)



      class UnitTest {
      constructor(unitTest: (this: UnitTest) => void);
      .
      .
      .
      util = Util; // Referes to the Util-Class (it's a class because its the only possibility in typescript for nesting


      The Util-class provides some general functions which are useful for writing tests



      class Util {
      static createFile(filePath: string): File;
      .
      .
      .
      }


      If you declare a Unit Test like this, everything works fine:



      var unitTest = new UnitTest(function() {
      this.util.createFile("..."); // VS-Code provides code completion for that functions of the unit-test-namespace
      });


      The problem is, that the util-class (which is used for
      code encapsulation) can be extended in every project. You can define a script following a specific name schema and the testframework dynamically loads that script and provides that functions in the util namespace, e.g. "test.util.myProject.js"



      module.exports = {
      myCustomUtilFunction = function () { ... }
      };


      Just by providing the script you can use that function in your unit test:



      var unitTest = new UnitTest(function() {
      this.util.myCustomUtilFunction();
      });


      But I can't cover this with the typescript definition file. VS-Code will not provide any code completion for the custom util functions because it's missing in the typescript definition of the test framework. I tried to extend the Util-class as follow, but VS-Code doesn't care:



      class Util {
      static myCustomUtilFunction(): void;
      }


      Any idea how to solve this?





      To make it easier to understand, here is the complete setup
      testframework.d.ts



      class UnitTest {
      constructor(unitTest: (this: UnitTest) =>

      util = Util;
      }

      class Util {
      static createFile(filePath: string): File;
      }


      myExtendedUtilNamespace.d.ts



      class Util {
      static myCustomUtilFunction(): void;
      }


      unitTests.js



      var unitTest = new UnitTest(function() {
      this.util.createFile("..."); // works
      this.util.myCustomUtilFunction(); // does not work
      });









      share|improve this question













      My company has developed a own test framework for the software, because it's not possible to use an open-source framework in the context of the software we develop.



      Anyway, I wrote Typescript Definition Files to improve the code completion for VS Code, but I can't make it work for the following setup:



      The following class is used to define unit tests. It provides some functions, e.g. for assertions. The main problem is the util-namespace. You can pass a function which is actually the unit test. Note that the this-object of that function points to the unit test class so that the passed function can make use of the functions of the unit test (internally the passed function will be called with "unitTest.call(this)" to set the this object of the function to the unit test instance)



      class UnitTest {
      constructor(unitTest: (this: UnitTest) => void);
      .
      .
      .
      util = Util; // Referes to the Util-Class (it's a class because its the only possibility in typescript for nesting


      The Util-class provides some general functions which are useful for writing tests



      class Util {
      static createFile(filePath: string): File;
      .
      .
      .
      }


      If you declare a Unit Test like this, everything works fine:



      var unitTest = new UnitTest(function() {
      this.util.createFile("..."); // VS-Code provides code completion for that functions of the unit-test-namespace
      });


      The problem is, that the util-class (which is used for
      code encapsulation) can be extended in every project. You can define a script following a specific name schema and the testframework dynamically loads that script and provides that functions in the util namespace, e.g. "test.util.myProject.js"



      module.exports = {
      myCustomUtilFunction = function () { ... }
      };


      Just by providing the script you can use that function in your unit test:



      var unitTest = new UnitTest(function() {
      this.util.myCustomUtilFunction();
      });


      But I can't cover this with the typescript definition file. VS-Code will not provide any code completion for the custom util functions because it's missing in the typescript definition of the test framework. I tried to extend the Util-class as follow, but VS-Code doesn't care:



      class Util {
      static myCustomUtilFunction(): void;
      }


      Any idea how to solve this?





      To make it easier to understand, here is the complete setup
      testframework.d.ts



      class UnitTest {
      constructor(unitTest: (this: UnitTest) =>

      util = Util;
      }

      class Util {
      static createFile(filePath: string): File;
      }


      myExtendedUtilNamespace.d.ts



      class Util {
      static myCustomUtilFunction(): void;
      }


      unitTests.js



      var unitTest = new UnitTest(function() {
      this.util.createFile("..."); // works
      this.util.myCustomUtilFunction(); // does not work
      });






      typescript class inner-classes extend overwrite






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 at 14:49









      maximus

      82




      82
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          To me is sounds like the Util class is never instantiated. If that is correct it shouldn't be a class. A namespace is more fitting in this context. Namespaces are also able to be merged between declaration files.



          // testframework.d.ts
          class UnitTest {
          constructor(unitTest: (this: UnitTest) => void);

          util = Util;
          }

          declare namespace Util {
          export function createFile(filePath: string): File;
          }

          // myExtendedUtilNamespace.d.ts
          declare namespace Util {
          export function myCustomUtilFunction(): void;
          }


          See the TypeScript Handbook about namespaces:
          https://www.typescriptlang.org/docs/handbook/namespaces.html






          share|improve this answer





















          • ups .. the emails of stackoverflow were pushed inside the spam-folder. You're right, there is no instance of the util class. I will try this next week and give feedback
            – maximus
            Nov 23 at 14:41










          • Ok, if I change the classes to namespaces, it works as excpected. Thank you!
            – maximus
            Dec 5 at 8:14











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302908%2foverwrite-extend-typescript-class-expressions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          To me is sounds like the Util class is never instantiated. If that is correct it shouldn't be a class. A namespace is more fitting in this context. Namespaces are also able to be merged between declaration files.



          // testframework.d.ts
          class UnitTest {
          constructor(unitTest: (this: UnitTest) => void);

          util = Util;
          }

          declare namespace Util {
          export function createFile(filePath: string): File;
          }

          // myExtendedUtilNamespace.d.ts
          declare namespace Util {
          export function myCustomUtilFunction(): void;
          }


          See the TypeScript Handbook about namespaces:
          https://www.typescriptlang.org/docs/handbook/namespaces.html






          share|improve this answer





















          • ups .. the emails of stackoverflow were pushed inside the spam-folder. You're right, there is no instance of the util class. I will try this next week and give feedback
            – maximus
            Nov 23 at 14:41










          • Ok, if I change the classes to namespaces, it works as excpected. Thank you!
            – maximus
            Dec 5 at 8:14















          up vote
          0
          down vote



          accepted










          To me is sounds like the Util class is never instantiated. If that is correct it shouldn't be a class. A namespace is more fitting in this context. Namespaces are also able to be merged between declaration files.



          // testframework.d.ts
          class UnitTest {
          constructor(unitTest: (this: UnitTest) => void);

          util = Util;
          }

          declare namespace Util {
          export function createFile(filePath: string): File;
          }

          // myExtendedUtilNamespace.d.ts
          declare namespace Util {
          export function myCustomUtilFunction(): void;
          }


          See the TypeScript Handbook about namespaces:
          https://www.typescriptlang.org/docs/handbook/namespaces.html






          share|improve this answer





















          • ups .. the emails of stackoverflow were pushed inside the spam-folder. You're right, there is no instance of the util class. I will try this next week and give feedback
            – maximus
            Nov 23 at 14:41










          • Ok, if I change the classes to namespaces, it works as excpected. Thank you!
            – maximus
            Dec 5 at 8:14













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          To me is sounds like the Util class is never instantiated. If that is correct it shouldn't be a class. A namespace is more fitting in this context. Namespaces are also able to be merged between declaration files.



          // testframework.d.ts
          class UnitTest {
          constructor(unitTest: (this: UnitTest) => void);

          util = Util;
          }

          declare namespace Util {
          export function createFile(filePath: string): File;
          }

          // myExtendedUtilNamespace.d.ts
          declare namespace Util {
          export function myCustomUtilFunction(): void;
          }


          See the TypeScript Handbook about namespaces:
          https://www.typescriptlang.org/docs/handbook/namespaces.html






          share|improve this answer












          To me is sounds like the Util class is never instantiated. If that is correct it shouldn't be a class. A namespace is more fitting in this context. Namespaces are also able to be merged between declaration files.



          // testframework.d.ts
          class UnitTest {
          constructor(unitTest: (this: UnitTest) => void);

          util = Util;
          }

          declare namespace Util {
          export function createFile(filePath: string): File;
          }

          // myExtendedUtilNamespace.d.ts
          declare namespace Util {
          export function myCustomUtilFunction(): void;
          }


          See the TypeScript Handbook about namespaces:
          https://www.typescriptlang.org/docs/handbook/namespaces.html







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 at 15:11









          Tim Lundqvist

          1366




          1366












          • ups .. the emails of stackoverflow were pushed inside the spam-folder. You're right, there is no instance of the util class. I will try this next week and give feedback
            – maximus
            Nov 23 at 14:41










          • Ok, if I change the classes to namespaces, it works as excpected. Thank you!
            – maximus
            Dec 5 at 8:14


















          • ups .. the emails of stackoverflow were pushed inside the spam-folder. You're right, there is no instance of the util class. I will try this next week and give feedback
            – maximus
            Nov 23 at 14:41










          • Ok, if I change the classes to namespaces, it works as excpected. Thank you!
            – maximus
            Dec 5 at 8:14
















          ups .. the emails of stackoverflow were pushed inside the spam-folder. You're right, there is no instance of the util class. I will try this next week and give feedback
          – maximus
          Nov 23 at 14:41




          ups .. the emails of stackoverflow were pushed inside the spam-folder. You're right, there is no instance of the util class. I will try this next week and give feedback
          – maximus
          Nov 23 at 14:41












          Ok, if I change the classes to namespaces, it works as excpected. Thank you!
          – maximus
          Dec 5 at 8:14




          Ok, if I change the classes to namespaces, it works as excpected. Thank you!
          – maximus
          Dec 5 at 8:14


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302908%2foverwrite-extend-typescript-class-expressions%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?