Can someone please explain, why in this code the assignment to the constant of type InterfaceA works, but the assignment to the constant of type InterfaceB throws an error:
interface InterfaceA {
doSomething (data: object): boolean;
}
interface InterfaceB {
doSomething: (data: object) => boolean;
}
function doIt (data: { type: string; }): boolean {
return true;
}
const A: InterfaceA = {
doSomething: doIt
};
const B: InterfaceB = {
doSomething: doIt
};
To me, both interfaces are defining the same, only the notation is different.
If this is not a bug in TypeScript, and there is a real reason, then let's come to my second question: I need to specify, that "doSomething" is optional and can either be a function, or a RegExp:
interface InterfaceB {
doSomething?: ((data: object) => boolean) | RegExp;
}`
How could I achieve this, with the notation of InterfaceA?