11

I am trying to unmarshal a dynamodb stream record with help of the official unmarshall function available in @aws-sdk/util-dynamodb.

I am doing this in typescript and the definition looks like this

unmarshall: (data: Record<string, AttributeValue>, options?: unmarshallOptions | undefined)

Here AttributValue is derived from @aws-sdk/client-dynamodb.

The incoming type of dynamodb record looks like this

export interface DynamoDBRecord {
    awsRegion?: string | undefined;
    dynamodb?: StreamRecord | undefined;
    eventID?: string | undefined;
    eventName?: 'INSERT' | 'MODIFY' | 'REMOVE' | undefined;
    eventSource?: string | undefined;
    eventSourceARN?: string | undefined;
    eventVersion?: string | undefined;
    userIdentity?: any;
}
export interface StreamRecord {
    ApproximateCreationDateTime?: number | undefined;
    Keys?: { [key: string]: AttributeValue } | undefined;
    NewImage?: { [key: string]: AttributeValue } | undefined;
    OldImage?: { [key: string]: AttributeValue } | undefined;
    SequenceNumber?: string | undefined;
    SizeBytes?: number | undefined;
    StreamViewType?: 'KEYS_ONLY' | 'NEW_IMAGE' | 'OLD_IMAGE' | 'NEW_AND_OLD_IMAGES' | undefined;
}
export interface AttributeValue {
    B?: string | undefined;
    BS?: string[] | undefined;
    BOOL?: boolean | undefined;
    L?: AttributeValue[] | undefined;
    M?: { [id: string]: AttributeValue } | undefined;
    N?: string | undefined;
    NS?: string[] | undefined;
    NULL?: boolean | undefined;
    S?: string | undefined;
    SS?: string[] | undefined;
}

Here Attribute value is coming from @types/aws-lambda/trigger/dynamodb-stream.d.ts

When I do unmarshall(dynamodbRecord.dynamodb.NewImage) I am getting the error

Argument of type '{ [key: string]: AttributeValue; }' is not assignable to parameter of type 'Record<string, AttributeValue>'.   Index signatures are incompatible.     Type 'AWSLambda.AttributeValue' is not assignable to type 'import("/Users/hitesh/learnapp/la-aws/node_modules/@aws-sdk/client-dynamodb/dist-types/models/models_0").AttributeValue'.       Property '$unknown' is missing in type 'AttributeValue' but required in type '$UnknownMember'.

Why do we have different definations of AttributeValue in @aws-sdk/client-dynamodb and @types/aws-lambda/trigger/dynamodb-stream.d.ts?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Hitesh Balwani
  • 175
  • 1
  • 12
  • Did you ever find a solution to this? I'm facing the exact same problem.. I don't understand why the types would be incompatible here. – Aaron Jun 28 '23 at 19:12

1 Answers1

2

I'm using a type assertion as a workaround here:

import type { AttributeValue } from '@aws-sdk/client-dynamodb'
// ...

unmarshall(dynamodbRecord.dynamodb.NewImage as Record<string, AttributeValue>)

I put a bounty on this question though, because I'd be happy to remove what should be an unnecessary type assertion!

Aaron
  • 2,049
  • 4
  • 28
  • 35