Skip to content

jsgen: maps are always constructed using string keys #24607

Description

@gchumillas

Describe the bug

Maps are always constructed using string keys, regardless of the original key type (which could be a number).

Reproduction Steps

Write a small main.v app:

fn map_keys[S, T](items map[S]T) []S {
	mut keys := []S{}
	for key, _ in items {
		keys << key
	}
	return keys
}

fn main() {
	items := {
		1: 'one'
		2: 'two'
		3: 'three'
	}

	JS.console.log(items)
	// As you can see, maps are constructed using the strings '1', '2', '3',
	// instead of the corresponding numbers.
	// {
	// 		map: { '1': { str: 'one' }, '2': { str: 'two' }, '3': { str: 'three' } },
	// 		length: 0
	// }

	// fails because maps are constructed using string keys, not number keys
	keys := map_keys(items)
	assert keys.contains(2)

}

Compile and execute it:

v -b js main.v
node main.js

Output:

{
  map: { '1': { str: 'one' }, '2': { str: 'two' }, '3': { str: 'three' } },
  length: 0
}
main.v:26: FAIL: fn main.main(): assert keys.contains(2)

Expected Behavior

The assertion should not fail.

Current Behavior

The assertion fails.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 d6a2a5e

Environment details (OS name and version, etc.)

V full version V 0.4.10 52c7130.d6a2a5e
OS macos, macOS, 15.5, 24F74
Processor 8 cpus, 64bit, little endian, Apple M1 Pro
Memory 0.56GB/16GB
V executable /Users/gonzalo/Projects/Personal/vlang/v
V last modified time 2025-05-29 16:43:24
V home dir OK, value: /Users/gonzalo/Projects/Personal/vlang
VMODULES OK, value: /Users/gonzalo/.vmodules
VTMP OK, value: /tmp/v_501
Current working dir OK, value: /Users/gonzalo/Projects/Personal/vlang
Git version git version 2.39.1
V git status weekly.2025.22-13-gd6a2a5e9
.git/config present true
cc version Apple clang version 17.0.0 (clang-1700.0.13.5)
gcc version Apple clang version 17.0.0 (clang-1700.0.13.5)
clang version Apple clang version 17.0.0 (clang-1700.0.13.5)
tcc version tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
tcc git status thirdparty-macos-arm64 e447816c
emcc version N/A
glibc version N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.Unit: JSBugs/feature requests, that are related to the JavaScript backend.

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions