Creating a discriminated union based on the first element of a non-empty array in TypeScript can ...

is it possible to create a discriminated union of the literal of the first element of a non empty array?

export const DynamoDBRecord = Schema.Struct({
  eventSource: Schema.Literal('aws:dynamodb'),
});
export type DynamoDBRecord = Schema.Schema.Type<typeof DynamoDBRecord>;

export const DynamoDBStreamEvent = Schema.Struct({
  Records: Schema.NonEmptyArray(DynamoDBRecord),
});
export type DynamoDBStreamEvent = Schema.Schema.Type<typeof DynamoDBStreamEvent>;

export const SQSRecord = Schema.Struct({
  eventSource: Schema.Literal('aws:sqs'),
});
export type SQSRecord = Schema.Schema.Type<typeof SQSRecord>;

export const SQSEvent = Schema.Struct({
  Records: Schema.NonEmptyArray(SQSRecord),
});
export type SQSEvent = Schema.Schema.Type<typeof SQSEvent>;


The AWS events for lambda are very difficult to "discriminate". How to capture this in Schema's?

(also see https://stackoverflow.com/questions/41814750/how-to-know-event-souce-of-lambda-function-in-itself/41837288#41837288)
Was this page helpful?