Could anyone clarify why the assignment to InterfaceA constant is successful while the assignment to InterfaceB constant results in 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
};
Check out this online demo for more context: Demo Link
Both interfaces seem similar, with just different notations. If this isn't a TypeScript bug and has a valid reason, let's move on to my next inquiry:
I want to specify that "doSomething" can be optional and can either be a function or a RegExp. How can I achieve this using the InterfaceA notation?
interface InterfaceB {
doSomething?: ((data: object) => boolean) | RegExp;
}