Hit*_*ani 15 amazon-web-services amazon-dynamodb typescript aws-lambda amazon-dynamodb-streams
我正在尝试借助unmarshall中提供的官方功能来解组 dynamodb 流记录@aws-sdk/util-dynamodb。
我正在打字稿中执行此操作,定义如下所示
\nunmarshall: (data: Record<string, AttributeValue>, options?: unmarshallOptions | undefined)
这里AttributValue是源自@aws-sdk/client-dynamodb.
dynamodb 记录的传入类型如下所示
\nexport interface DynamoDBRecord {\n awsRegion?: string | undefined;\n dynamodb?: StreamRecord | undefined;\n eventID?: string | undefined;\n eventName?: 'INSERT' | 'MODIFY' | 'REMOVE' | undefined;\n eventSource?: string | undefined;\n eventSourceARN?: string | undefined;\n eventVersion?: string | undefined;\n userIdentity?: any;\n}\nRun Code Online (Sandbox Code Playgroud)\nexport interface StreamRecord {\n ApproximateCreationDateTime?: number | undefined;\n Keys?: { [key: string]: AttributeValue } | undefined;\n NewImage?: { [key: string]: AttributeValue } | undefined;\n OldImage?: { [key: string]: AttributeValue } | undefined;\n SequenceNumber?: string | undefined;\n SizeBytes?: number | undefined;\n StreamViewType?: 'KEYS_ONLY' | 'NEW_IMAGE' | 'OLD_IMAGE' | 'NEW_AND_OLD_IMAGES' | undefined;\n}\nRun Code Online (Sandbox Code Playgroud)\nexport interface AttributeValue {\n B?: string | undefined;\n BS?: string[] | undefined;\n BOOL?: boolean | undefined;\n L?: AttributeValue[] | undefined;\n M?: { [id: string]: AttributeValue } | undefined;\n N?: string | undefined;\n NS?: string[] | undefined;\n NULL?: boolean | undefined;\n S?: string | undefined;\n SS?: string[] | undefined;\n}\nRun Code Online (Sandbox Code Playgroud)\n这里的Attribute价值来自于@types/aws-lambda/trigger/dynamodb-stream.d.ts
当我这样做时,unmarshall(dynamodbRecord.dynamodb.NewImage)我收到错误
\n\n类型参数 '{ [key: string]: AttributeValue; }' 不可分配给类型为“Record<string, AttributeValue>”的参数。\xc2\xa0\xc2\xa0索引签名不兼容。\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0Type 'AWSLambda.AttributeValue' 不可分配给类型 'import("/Users/hitesh/learnapp/la-aws/node_modules/@aws-sdk/client- dynamodb/dist-types/models/models_0").AttributeValue'。\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0Property '$unknown' 在类型“AttributeValue”中缺失,但在类型“$UnknownMember”中必需。
\n
为什么我们对AttributeValuein@aws-sdk/client-dynamodb和的定义不同@types/aws-lambda/trigger/dynamodb-stream.d.ts?
我在这里使用类型断言作为解决方法:
import type { AttributeValue } from '@aws-sdk/client-dynamodb'
// ...
unmarshall(dynamodbRecord.dynamodb.NewImage as Record<string, AttributeValue>)
Run Code Online (Sandbox Code Playgroud)
我很乐意删除不必要的类型断言!