0

Why do I get error "the string is not assignable to iGreet" ?

  interface iGreet {
    (p: string ): string
  }


  class C {
    greeting: string;

    constructor(message: string) {
      this.greeting = message;
    }

    greet(p: string): iGreet {
      return "Hello, " + this.greeting + " " + p;
    } 
  }

  let greeter = new C("world");
  console.log(greeter.greet("test"))
1
  • 1
    You're saying the method greet should return an iGreet, but right now it returns a string. Commented Sep 27, 2022 at 22:54

1 Answer 1

1

greet(p: string): iGreet is saying this function returns iGreet. If you want to annotate the type of greet, you'll need to use an assignment like this:

greet: iGreet = function (p) {
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.