fix(util-dynamodb): narrow NativeAttributeValue to remove implicit any (#7526)#7989
Open
mohanrajvenkatesan23-04 wants to merge 1 commit into
Conversation
aws#7526) The NativeAttributeValue union included InstanceType<{ new (...args: any[]): any }>, which TypeScript widens to any and caused the entire union to collapse to any, silently disabling type-checking for every consumer of the DynamoDB Document Client. Remove that union member and adjust three internal casts in convertToAttr.ts, marshall.ts, and unmarshall.ts that surfaced once the implicit any was no longer absorbing them. Type-only change; runtime behavior is unchanged. Adds packages-internal/util-dynamodb/src/NativeAttributeValue.spec.ts with positive assignments for all supported native types and @ts-expect-error regressions to guard against the union collapsing to any again.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Fixes #7526 —
NativeAttributeValuetype resolves toany.Description
The
NativeAttributeValueunion exported from@aws-sdk/util-dynamodb(and re-exported by@aws-sdk/lib-dynamodb) includedInstanceType<{ new (...args: any[]): any }>as a union member. TypeScript widensInstanceType<{ new (...args: any[]): any }>toany, which collapsed the entire union toanyand silently disabled type-checking for every consumer of the DynamoDB Document Client (PutCommand,UpdateCommand,marshall,convertToAttr, etc.).The maintainer (@kuhe) identified the fix in a comment on the issue: remove that member from the union. This PR removes it and resolves the latent typecheck errors that surfaced inside the package once the implicit
anywas no longer absorbing them — three small internal cast adjustments inconvertToAttr.ts,marshall.ts, andunmarshall.ts. No runtime behavior changes.Files changed:
packages-internal/util-dynamodb/src/models.ts— the actual type fix (drop theInstanceType<...>union member that widened toany).packages-internal/util-dynamodb/src/convertToAttr.ts— small local-cast adjustment to satisfy the now-stricter union.packages-internal/util-dynamodb/src/marshall.ts— small cast adjustment.packages-internal/util-dynamodb/src/unmarshall.ts— small cast adjustment.packages-internal/util-dynamodb/src/NativeAttributeValue.spec.ts— new regression spec.Note on breaking-ness: This is a type-only change. Runtime behavior is identical. Downstream users currently passing arbitrary class instances directly into
marshall/convertToAttr(relying onconvertClassInstanceToMap) will now see compile errors at the call site. The maintainer flagged the original bug as "breaking"; this PR follows their guidance.generated by AI tools, and reviewed by Mohanraj Venkatesan
Testing
Added
packages-internal/util-dynamodb/src/NativeAttributeValue.spec.tswith 16 Vitest cases:NumberValue,Uint8Array,ArrayBuffer,Setvariants, map, list).// @ts-expect-errorregressions that verify the union no longer collapses toany(function, symbol, and a parameter-not-narrowed case).Verified the regression tests catch the original bug: temporarily restoring the offending union member produces 3
TS2578: Unused '@ts-expect-error' directiveerrors, exactly as expected.Local results:
npx vitest runinpackages-internal/util-dynamodb— 388/388 pass (372 pre-existing + 16 new).npx tsc --noEmit -p tsconfig.types.json— only pre-existing baseline errors remain (4@aws-sdk/client-dynamodbmodule-resolution errors due to missing localdist-*/outputs, plus one pre-existing unused-directive). No new errors introduced.Checklist
NativeAttributeValuetype isany#7526).mainand passes with this fix.vitest run: 388/388).tsc --noEmiterrors introduced.