1

I have a class that needs to implement a method that accepts two or one params based on the child class.

class MyClass { 
    update(obj: HashMap);
    update(id: ID, obj: HashMap);
    update(objOrId: HashMap | ID, obj?: HashMap) {
      console.log('here I want to use only the first param');
    }
}

class SubClass extends MyClass {
    update(id: ID, obj: HashMap) {
      console.log('here I want to use both params');
    }
}

The problem is that the SubClass yelling at me that the update signature is not compatible with the base class. How can I achieve this?

2 Answers 2

2

You should get the following error:

Property 'update' in type 'SubClass' is not assignable to the same property in base type 'MyClass'.

Type '(id: ID, obj: HashMap) => void' is not assignable to type '{ (obj: HashMap): any; (id: ID, obj: HashMap): any; }'.

The reason for this is that when overriding a method, it has to have the exact same type as the original method. Since the method in MyClass is an overloaded type, it has two alternative types. But the overriden method only has one.

So you need to specify the exact same overloads, even if you don’t need them in the subtype:

class SubClass extends MyClass {
    update(obj: HashMap);
    update(id: ID, obj: HashMap);
    update(objOrId: HashMap | ID, obj?: HashMap) {
      console.log('here I want to use both params');
    }
}

Remember that TypeScript will actually only emit the final function definition, and there is no run-time validation for the parameter types. So you will still have to handle the case that just a HashMap will be passed to your SubClass instance. This is especially true if you consider the Liskov substitution principle which also requires that any instance of SubClass can be used as a MyClass instance; so passing just a HashMap is absolutely fine.

Example on TypeScript playground

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

8 Comments

The update method of the subclass still shows the one params approach. I need there the two params approach.
I have no idea what you are trying to say with that. You can totally call the method with two arguments on the subclass. See the linked example.
I can call them, you are right, but the autocompletion shows always the one value approach
see the signature of the auto-completion in your live example.
No, when you type the sub.update(, you will get the auto completion pop-up and then you can move the cursor up and down to cycle through the various overloads.
|
2

You need to add the public overload signatures to your class. The implementation signature is not visible to anyone (not even inside MyClass) so the compatibility that is checked is for the public signatures, and since the compiler can't find them it issues an error:

class SubClass extends MyClass {
    update(obj: HashMap) : void;
    update(id: ID, obj: HashMap): void;
    update(objOrId: HashMap , obj?: HashMap): void{
    console.log('here I want to use both params');
    }
}

1 Comment

The update method of the subclass still shows the one params approach. I need there the two params approach.

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.