I'm receiving a type error when using an adapter as in this reduced example (see last line of method getGloryOfAnimal). I'm puzzled because, as far as I can tell, the types are fully explicated.
interface ICheetah {
pace: string;
}
interface ILion {
mane: string;
}
let LionAdapter = {
endpoint: 'lion',
castRawData: (d: any) => d as ILion,
getValue: (d: ILion) => d.mane
}
let CheetahAdapter = {
endpoint: 'cheetah',
castRawData: (d: any) => d as ICheetah,
getValue: (d: ICheetah) => d.pace
}
type AnimalAdapter = typeof CheetahAdapter | typeof LionAdapter;
function getDataFromEndpoint(endpoint: string): any {
// data comes back in a format from the server
// synchronous here for simplicity
if (endpoint === 'cheetah') {
return {
pace: 'lightning speed'
};
} else {
return {
mane: 'shiny mane'
};
}
}
function getGloryOfAnimal(adapter: AnimalAdapter): string {
let data = adapter.castRawData(getDataFromEndpoint(adapter.endpoint));
// type error below:
// 'cannot invoke expression whose type lacks a call signature'
return adapter.getValue(data);
}
console.log(getGloryOfAnimal(LionAdapter));
I believe I could write an interface for the two adapters rather than creating a union type (e.g. (T | U)), but in my case the interface would be very large.
Thoughts? Am I stuck with creating a huge common interface for the adapters?