Our application is designed to efficiently handle the different structures of an array of objects by utilizing a large conditional type based on specific object properties.
The array of objects dictates a sequence of actions to be performed, each action having unique properties that our engine interprets to execute corresponding code. The decision-making process relies on a comprehensive map that runs code based on the type
property, as shown in the example below.
Here's a simplified representation:
type payloadA = {
propA: string;
}
type payloadB = {
propB: boolean;
}
enum Payloads {
A = 'A',
B = 'B',
}
type conditionalPayload<T extends Payloads> =
T extends Payloads.A ? payloadA
: T extends Payloads.B ? payloadB
: never;
type myObj<T extends Payloads> = {
type: T;
payload: conditionalPayload<T>
}
// Array of arrays structure used within our code implementation
const myArray: myObj<Payloads>[][] = [
[
{
type: Payloads.A,
payload: {
propA: 'dummy',
},
},
{
type: Payloads.B,
payload: {
propA: 'dummy', // Currently does not trigger an error, but we aim for validation
},
},
],
];
We are striving for a type checker that can validate the payload
property during code development and prevent such errors from slipping through.