Skip to content

fix: allow prototype method names as object keys#1586

Open
kaigritun wants to merge 1 commit intoarktypeio:mainfrom
kaigritun:fix/constructor-key-duplicate-error
Open

fix: allow prototype method names as object keys#1586
kaigritun wants to merge 1 commit intoarktypeio:mainfrom
kaigritun:fix/constructor-key-duplicate-error

Conversation

@kaigritun
Copy link

Summary

Fixes #1572

Object.prototype method names like constructor, hasOwnProperty, and toString are valid object keys and should not be incorrectly flagged as duplicate keys.

Problem

When defining an object type with a key that matches an Object.prototype method name:

type({ constructor: "string" })

This throws a ParseError: Duplicate key "constructor" even though the key is valid.

Root Cause

The seen object used for duplicate key detection was initialized with {}, which inherits from Object.prototype. When checking key in seen, it returned true for keys like "constructor" because they exist on the prototype chain.

Solution

Use Object.create(null) to create a prototype-free object for the seen tracking. This ensures only explicitly set keys are detected as duplicates.

Testing

Added a test case to verify that prototype method names can be used as object keys without error.

Object.prototype method names like 'constructor', 'hasOwnProperty',
and 'toString' are valid object keys and should not be incorrectly
flagged as duplicate keys.

The issue was that the 'seen' object used for duplicate detection was
initialized with '{}' which has prototype methods. When checking
'key in seen' for these keys, it returned true even though they
weren't explicitly set.

Fix: Use Object.create(null) to create a prototype-free object for
the 'seen' tracking.

Fixes arktypeio#1572
@pullfrog
Copy link
Contributor

pullfrog bot commented Feb 8, 2026

Leaping into action...

Pullfrog  | View workflow run | Triggered by Pullfrogpullfrog.com𝕏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

Cannot define key "constructor" in object literal (ParseError: Duplicate key)

1 participant