Skip to content

JSONObject should support narrowing to [key: number]:JSONValue #1689

Closed
@codyfrisch

Description

@codyfrisch

Use case

Behind the scenes, javascript converts numeric keys to strings. The pattern object[1234] is valid. However, JSONObject is defined as [key: string]: JSONValue and the result of getMultiple (dynamodb provider) cannot be cast into a type like [key: number] : JSONValue since string cannot be narrowed to number.

The type being used expects it to be a number, because that is how the parameter is accessed.

You can do object[1234] today without this change because TypeScript knows about the behavior of numbers being converted to strings. This is simply about narrowing the type to only allow numbers.

Solution/User Experience

Update JSONObject to the following type: type JSONObject = { [key: string | number]: JSONValue; }

This way a type can narrow JSONObject to [key: number]:JSONValue.

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilitygood-first-issueSomething that is suitable for those who want to start contributing

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions