dopatraman dopatraman - 3 months ago 103
TypeScript Question

Error when instantiating class: Supplied parameters do not match any signature of call target

I have a class here:

export class MyClass {
public name:string;
public addr:string;

constructor() {}
}


And I import it here:

import { MyClass } from './MyClass';

// and use it here:

class MyUser {
private _prop : MyClass[];

constructor() {
this._prop = [
new MyClass({name: 'Hello', addr: 'World'}) //<--- this is where the error appears
]
}
}


When I do this I get a linting error:

Supplied parameters do not match any signature of call target


Why can't I instantiate my class?

Answer

You haven't had mention any parameter in your MyClass constructor. You have to put parameter in constructor so that you can set value while instantiating this class. You could move MyClass properties to constructor parameter to make it shortened syntax like below.

export class MyClass {
    //by having `public` on constructor shortened the syntax.
    constructor(public name: string, public addr:string) {

    }
}   

constructor() {
    this._prop = [
        new MyClass('Hello', 'World')
    ]
}

Playground Demo

Comments