Simon Dean
Simon Dean

Reputation: 237

Typescript Constructor Class not behaving as expected, "0 Arguments Expected"

I have a constructor class, it should take in two numbers as arguments and build an object with two props and a single method.

export class Context{
    public input1: number;
    public input2: number;

    Constructor(num1: number, num2: number) {
        this.input1 = num1; 
        this.input2 = num2;
    }

    display() {
        console.log(this.input1, this.input2); 
    }
}

I instantiate the class, and attempt to pass in two values as arguments as follows:

import { Context } from "./context";

class App {
    public static main() {
        let context: Context = new Context(2,7); 
        context.display();
    }
}

App.main();

However, my IDE is stating that the new Context call for instantiation does not expect any arguments to be passed into it, this is not correct and I do not understand why I am receiving this error.

Thanks

Upvotes: 1

Views: 1242

Answers (1)

Majesty
Majesty

Reputation: 1909

That's because register is important.

Constructor from capital C is not recognized as class constructor function, I think it is treated as a method.

Therefore, there is no constructor defined for the Context class, so you reasonably getting an error... Just rename Constructor to constructor...

Upvotes: 2

Related Questions