Skip to content

fix: allow prototype method names as object keys#1586

Merged
ssalbdivad merged 1 commit into
arktypeio:mainfrom
kaigritun:fix/constructor-key-duplicate-error
Jul 7, 2026
Merged

fix: allow prototype method names as object keys#1586
ssalbdivad merged 1 commit into
arktypeio:mainfrom
kaigritun:fix/constructor-key-duplicate-error

Conversation

@kaigritun

Copy link
Copy Markdown
Contributor

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

pullfrog Bot commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

Leaping into action...

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

@ssalbdivad
ssalbdivad merged commit eb93f33 into arktypeio:main Jul 7, 2026
@github-project-automation github-project-automation Bot moved this from To do to Done (merged or closed) in arktypeio Jul 7, 2026
@ssalbdivad

Copy link
Copy Markdown
Member

sorry for the delay merging this, good find!

ssalbdivad added a commit that referenced this pull request Jul 7, 2026
Bumps all publishable packages and documents the fixes merged since 2.2.2:

- fix type.fn.raw throwing at runtime (#1631, @aarsh767)
- anchor versioned UUID validation (#1602, @WolfieLeader)
- fix inferred output of declared morphs (#1579, @eralmansouri)
- allow Object.prototype method names as keys (#1586, @kaigritun)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qk4c4vPUecBDYbd4p3yE1H
ssalbdivad added a commit that referenced this pull request Jul 7, 2026
Bumps all publishable packages and documents the fixes merged since 2.2.2:

- fix type.fn.raw throwing at runtime (#1631, @aarsh767)
- anchor versioned UUID validation (#1602, @WolfieLeader)
- fix inferred output of declared morphs (#1579, @eralmansouri)
- allow Object.prototype method names as keys (#1586, @kaigritun)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done (merged or closed)

Development

Successfully merging this pull request may close these issues.

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

2 participants