1

Just trying to learn TypeScript.

Not sure why I'm still getting below error.

Note: I have tried meeting all the requirements of interface

interface Greet {
    greet(name?: Greet): string;
    (val: string): string;
}


class Person implements Greet {

    greet(name?: Greet): string {
        return 'Hello ' + name;
    }

    obj(val: string):string {
        return 'Hello';
    };


}

Error

TsFiles/OopsTest.ts(8,7): error TS2420: Class 'Person' incorrectly implements interface 'Greet'.
  Type 'Person' provides no match for the signature '(val: string): string'
8:26:50 PM - Compilation complete. Watching for file changes.
0

2 Answers 2

1

If you're trying to create a hybrid type then, per the documentation, the implementation should look something like:

function getPerson(): Greet {
    let person = <Greet>function(val: string) { return '' };
    person.greet = function(name?: Greet) { return '' };
    return person;
}

Having (val: string): string; in the interface requires the implementation of Greet to be a function, not a class.

Sign up to request clarification or add additional context in comments.

1 Comment

@jonrsharpe..Thank you.
0
interface Greet {
    greet(name?: Greet): string;
    obj(val: string): string; // <<<=== `obj` missing
}

2 Comments

.. I'm trying to implement interface with bare functionality

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.