Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

typescript remind cannot assign type '(x: string) => void' to type '() => void

I used the code, defined an interface, and made a function, but when the subclass uses the function defined by the interface, an error occurs, prompting cannot assign type ‘(x: string) => void’ to type ‘() => void

interface People{
  speak():void;
}

class Anny implements People {
  name:string = 'anny';

  speak(x:string):void {
    console.log(this.name, x);
  }
}
const annyObj = new Anny();
annyObj.speak('hello');

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have to change the speak method parameter to nullable:

class Anny implements People {
    name: string = 'anny'
    // x is nullable
    speak(x?: string): void {
        console.log(this.name, x)
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading