Our application uses a big conditional type to handle the possible structures of a given array of objects based on one of the properties of said objects.
We use the array of objects to define a sequence of actions to run, these actions can have different properties which our engine uses to run the corresponding code. We then use a big map to run code based on the type
property in the example below.
Here is a simplified sample:
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>
}
// Using an array of arrays because that is how it is in our code
const myArray: myObj<Payloads>[][] = [
[
{
type: Payloads.A,
payload: {
propA: 'dummy',
},
},
{
type: Payloads.B,
payload: {
propA: 'dummy', // I would like this to throw an error, currently it does not
},
},
],
];
We’d like the type checker to be able to validate the payload
property while writing the code.