-1

A function should return object { a: number, b: number }, so I'm creating the object and fill it up later:

const output = {};
output.a = 1;
output.b = 2;
return output;

But TS gives me the error: Type '{}' is missing the following properties from type 'Output': a, b ts(2739)

I'm expecting TS to understand I've added required fields to the const object as the function is running.

TotalAMD
  • 887
  • 1
  • 10
  • 20

1 Answers1

0

TypeScript compiler doesn't consider the added properties based on runtime. One way to do it is to use the as keyword as follows:

const output = {} as { a: number, b: number };
AlefiyaAbbas
  • 235
  • 11