microsoft / microsoft/TypeScript

JS `Object.defineProperty` declaration can't define divergent accessors

Open
#55,505 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: JavaScript Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

divergent accessors setter getter expando js jsdoc

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play?filetype=js#code/MYewdgzgLgBAHjAvDA3gXwNwCgsHkBGAVgKbBQB0AJsQGYCWYxACgE4gAOxLUAngBRwANDABEUYtBHCUWGDAD0AKkUwAAi2JQAri0ipoLBgHM0MRfNkwjmvgEpUluRu27RI7HLSDLSlavYAhiwBALaoYFoh+FymAWYWchA2Afbo3mi22Fg0WmBkdOAwdBAgADYB4gBibCEAonCBYJQgAIIQEHRGYCHEYFAQAEIMlMZ2DnJw5OLQSDAALABMGAryMFxsLDD4WrAQABYgWqWUW8QwuADSWGhYQA

💻 Code
const x = {};

Object.defineProperty(x, "test", {
  /** @returns {string} */
  get() {
    return "";
  },
  /** @param {number} a */
  set(a) {},
});

function isolateFromExpandoAssignmentsBinding() {
  x.test = 42; // error but should be OK
}
🙁 Actual behavior

The write type of this expando member is string

🙂 Expected behavior

The write type of this expando member should be number since divergent accessors are allowed nowadays

Additional information about the issue

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the playground link and the minimal Object.defineProperty example to reproduce the divergent getter and setter types. Trace how the TypeScript type checker determines the write type for this expando member, then verify that x.test accepts number while preserving the getter's string type.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.