Why am I encountering an error in TypeScript when using Object destructuring?
The JavaScript code executes without any issues, but TypeScript is showing errors.
fn
error:
This expression is not callable.
Not all elements of type '(() => void) | { bar: () => void; }' are callable.
Type '{ bar: () => void; }' does not have call signatures.
bar
error:
Property 'bar' is not found in type '(() => void) | { bar: () => void; }'
Check out the code on StackBlitz
https://i.sstatic.net/r5Liz.png
const foo = () => {
const fn = () => { console.log('in fn'); };
return [{ bar: () => { console.log('in bar'); } }, fn];
};
const baz = () => {
const [{ bar }, fn] = foo();
fn();
};
baz();