microsoft / microsoft/TypeScript

Getting a setter-only property is typed as the setter's parameter's type instead of undefined

Open
#60,954 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 Search Terms

setter only

🕗 Version & Regression Information
  • This is the behavior in every version I tried, from 5.7.3 down to 3.3.3333
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/MYewdgzgLgBANgQzAcwK4OQUxgXhgbwCgYYJNZhUAnKzMKACjAQFtMAuUqKgSxQEoCxEjCgALHhAB0cEMikAHVBDFNWmfgG5hAXwA0w2ck4BtALowEELrxTmDO7YUQp0WKZRp1YeAEQAxACVfJ1BIClx4JDQMTA9qWnptIA

💻 Code
const language = {
  set current(name: string) {
    this.log.push(name);
  },
  log: [] as string[],
};

language.current = "FR";

const c = language.current;
🙁 Actual behavior

Variable c is said to be of type string.

🙂 Expected behavior

As clearly stated here, it should be undefined.

Note that current is not defined, and any attempts to access it will result in undefined.

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 with the linked TypeScript Playground example and confirm that reading the setter-only property is typed as string rather than undefined. Trace the compiler logic that determines the type of an accessor read, then add coverage for this object-literal setter case and verify that the inferred type is undefined without regressing valid getter/setter behavior.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.