I have a script that works a bit like an assembly line. It starts with a dictionary with an object with only one property and adds one more property after each step.
I started with plain JS and am now converting this to TS.
I have modeled this now as a Typescript interface with one mandatory property and a lot of optional properties:
interface CarBatch {
[key: string]: {
part1: string;
part2?: string[];
part3?: SteeringWheel;
part4?: string;
part5?: string;
part6?: string;
}
}
However, Typescript complains a lot about this, as accessing these optional properties always yields "Object is possibly 'undefined'". For example console.log(carBatch['porsche'].part4)
I tried to use a base interface and extends but this was difficult because of the dictionary and because my assembly steps always would start with one kind of type and would need to modify pieces of it to another type in the middle.
What would be the correct way to model this?