1

I'm writing a program using TypeScript. The problem is I implemented HTMLElement interface.

export class IEElement implements HTMLElement {
   // something here
}

The compiler shows many errors that I have some properties missing (IEElement declares an interface but does not implement it). I have implemented about 5 properties that I need to. The rest is redundant. How to avoid errors? Do I need to implement all the interface members?

1 Answer 1

2

Yes, you need to implement all non-optional interface members.

The interface is a contract, if you have a class that implements that contract you are promising to implement everything in that contract.

The HTMLElement interface has a lot to implement - but if you just want to add a bit of behaviour, perhaps you could start with an existing implementation...

interface SpecialElement extends HTMLElement {
    myCustomFunction: () => void;
}

var element = <SpecialElement>document.getElementById('example');

element.myCustomFunction = function () { };
Sign up to request clarification or add additional context in comments.

2 Comments

It's pretty bad:/ Now I have to implement about 70 members... Thanks for the answer. Have a good day.
@Nickon I've just added a potential solution for you that might make it a bit easier - depending on what you are doing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.