Why Does This Typescript Output “[Class] is not a constructor.”?

后端 未结 12 1263
清歌不尽
清歌不尽 2021-01-17 07:55

I\'m working in typescript 1.5 in visual studio. I have a main class called app.ts, and another called FizzBuzzManager.ts. I can\'t figure out what is wrong with this code,

12条回答
  •  暖寄归人
    2021-01-17 08:46

    For future readers, the problem could also be if the constructor expects parameters and you are giving no parameters or a different number of parameters. This is because for Javascript a function with a different number of parameters is a different function.

    A simple solution could be to add default parameters in the constructor.

    My example was like this:

    SegmentsQueryBuilder.ts

    class SegmentsQueryBuilder {
      //...
      constructor(scoping) {
        //...
      }
    //...
    }
    

    segments.query.builder.test.ts

    describe('Segment base query', () => {
      test('should create segment select with default fields', () => {
        const segmentQueryBuilder = new SegmentQueryBuilder() //ERROR HERE
    //...
    

    The solution here was either to use default parameter in the constructor or to pass the scoping object in the usage of the constructor

提交回复
热议问题