Describing an Empty Object Type
.
This type represents an object with no members of its own. TypeScript will throw a compile-time error if you attempt to access arbitrary properties on such an object:
// Type {}
const obj = {};
// Error: Property 'prop' does not exist on type '{}'.
obj.prop = "value";
Despite this limitation, you can still utilize all properties and methods defined on the Object type, which are accessible via JavaScript's prototype chain:
// Type {}
const obj = {};
// "[object Object]"
obj.toString();
An insightful read on Basarat's Lazy Object Initialization, explaining how Typescript handles this scenario and offering solutions.
To adjust your code accordingly, follow this example:
interface Foo {
bar: string;
baz: number;
}
private getJsonBody(body: {} as Foo | FormData) {
return !(body instanceof FormData)
? JSON.stringify(body)
: body;
}