#What am I missing here?

1 messages ยท Page 1 of 1 (latest)

vagrant inletBOT
#
gencha#0069

Preview:```ts
export interface SQSRecord {
body: string
}

export interface AWSSQSEvent {
Records: SQSRecord[]
}

export type SQSEvent<
TSchema,
TBase extends AWSSQSEvent = AWSSQSEvent

= Omit<TBase, "Records"> & {
Records: Array<Omit<AWSSQSEvent["Records"], "body">
...```

tough dock
#

moment

#

i'mma assume the rubber duck solved this one

#

!close

vagrant inletBOT
#

@thick epoch
Because your issue seemed to be resolved, this post was marked as resolved by @tough dock.
If your issue is not resolved, you can reopen this post by running !reopen.
If you have a different question, make a new post in #1057653400046674112.

tough dock
#

well

#

idk

thick epoch
#

It's not solved ๐Ÿ˜„

vagrant inletBOT
#
gencha#0069

Preview:```ts
export interface SQSRecord {
body: string
}

export interface AWSSQSEvent {
Records: SQSRecord[]
}

export type SQSEvent<
TSchema,
TBase extends AWSSQSEvent = AWSSQSEvent

= Omit<TBase, "Records"> & {
Records: Array<Omit<AWSSQSEvent["Records"], "body">
...```

thick epoch
#

I'm only getting more confused

#

!reopen

tough dock
#

ah

#

apologies, it just looks weird cuz the original message was deleted

thick epoch
#

Yeah, it was only the playground link

tough dock
#

anyway the issue is that it's two separate array types

#

typescript doesn't merge that into one single array type

thick epoch
#

Hmm aha

tough dock
#

and so when typescript looks up the methods, it looks them up on one of the types

tough dock
thick epoch
#

Makes sense

tough dock
#

so the actual reason is just that typescript doesn't do it because it doesn't think of every possibility

#

one way i'd solve it would be to have a generic parameter that goes all the way down to SQSRecord

thick epoch
vagrant inletBOT
#
n_n#2622

Preview:```ts
...
export interface SQSRecord<T = string> {
body: T
}

export interface AWSSQSEvent<T = string> {
Records: SQSRecord<T>[]
}

export type SQSEvent<
TSchema,
TBase extends AWSSQSEvent<unknown> = AWSSQSEvent<
TSchema extends AWSSQSEvent<infer T> ? T : string

= TBase & TSchema
...```

tough dock
#

something like this

#

depending on your actual code you may have to modify it a bit of course

thick epoch
#

Sadly, I have no control over AWSSQSEvent. That's from an external library

tough dock
#

๐Ÿ˜”

#

is TSchema guaranteed to have Records: { body: something; }[]?

tough dock
#

otherwise the best (?) solution may be to use a conditional type

export type SQSEvent<TSchema, TBase extends AWSSQSEvent = AWSSQSEvent> =
  TSchema extends GeneralAWSSQSEvent
? Omit<TBase, "Records"> & TSchema
: TBase & TSchema;
thick epoch
#

Let me check out your suggestion, I was just at lunch

#

Definitely got my brain on the right track. Right now, I'm favoring this:

import { ArrayElement } from "@dynmedia/lib-tools";
import { SQSEvent as AWSSQSEvent } from "aws-lambda";

export type SQSEvent<
  TSchema extends { Records: Array<unknown> },
  TBase extends AWSSQSEvent = AWSSQSEvent
> = Omit<TBase, "Records"> & {
  Records: Array<
    Omit<ArrayElement<AWSSQSEvent["Records"]>, "body"> & ArrayElement<TSchema["Records"]>
  >;
};
tough dock
#

ArrayElement<T> aka T[number]