I have several functions that all take the same type as input but return different types of interfaces. I'd like to define a type that can encompass all these functions, but when I try to do so with:
const f: (arg: number) => Object = func;
I encounter an error:
Type (arg: number) => SomeInterface is not compatible with type (arg: number) => Object
I could simply use (arg: number) => any
, but then it defeats the purpose of having strict typing. What I really want is for it to accept any Object and not just a specific interface because I have multiple interfaces.
For example, I am working on communication protocols:
interface splPacket {
serviceType: number,
serviceSubType: number,
satelliteTime: Date,
data: Buffer
}
function decodeSpl(input: Buffer): splPacket {...}
function encodeSpl(input: splPacket): Buffer {...}
const spl = {
decode: decodeSpl,
encode: encodeSpl
};
interface ax25Packet {
destCallsign: string,
destSSID: number,
srcCallsign: string,
srcSSID: number,
data: Buffer
}
function decodeAx25(input: Buffer): ax25Packet {...}
function encodeAx25(input: ax25Packet): Buffer {...}
const ax25 = {
decode: decodeAx25,
encode: encodeAx25
};
interface commProtocol {
decode: (input: Buffer): Object;
encode: (input: Object): Buffer;
};
const protocol: commProtocol = spl;
However, spl
and ax25
are not compatible with commProtocol
.
I need to create an interface that can accommodate all my communication protocol implementations.
What should be my next step?