4

In the following code, I'm getting an error on implementing the abstract method getPrice.

abstract class Exchange {
    public coins=[];
    abstract getPrice();
}
class Binance extends Exchange {
    getPrice(coin,stable_coin){
       return  axios.get(`https://api.binance.com/api/v3/avgPrice?symbol=${coin.symbol}${stable_coin.symbol}`).then(res=>res.data.price)
    }
}
class Bittrex extends Exchange {
    getPrice(coin,stable_coin){
        return  axios.get(`https://api.bittrex.com/api/v1.1/public/getticker?market=${stable_coin.symbol}-${coin.symbol}`).then(res=>res.data.result.Last);
     }
}

I'm getting the following error:

Property 'getPrice' in type 'Binance' is not assignable to the same property in base type 'Exchange'. Type '(coin: any, stable_coin: any) => any' is not assignable to type '() => any'.ts(2416)

2 Answers 2

2

You need to match the parameters of the abstract method as well. your derived classes are passing arguments that arent defined in the base class.

abstract class Exchange {
    public coins=[];
    abstract getPrice(coin:any, stable_coin: any): any;
}
class Binance extends Exchange {
    getPrice(coin: any, stable_coin: any): any {
       return  axios.get(`https://api.binance.com/api/v3/avgPrice?symbol=${coin.symbol}${stable_coin.symbol}`).then(res=>res.data.price)
    }
}
class Bittrex extends Exchange {
    getPrice(coin: any, stable_coin: any): any {
        return  axios.get(`https://api.bittrex.com/api/v1.1/public/getticker?market=${stable_coin.symbol}-${coin.symbol}`).then(res=>res.data.result.Last);
     }
}

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

Comments

1

This is a common class inheritance expectation: overriding methods should have a compatible signature with the base (super) method.

Here you could have e.g. abstract getPrice(coin: any, stable_coin: any) in your abstract class.

Or, depending if it makes sense in your case or not, have the extra parameters of your child methods be optional.

Comments

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.