microsoft / microsoft/TypeScript

No error when accessing setter-only properties

Open
#58,112 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

"setter only", "setter-only"

🕗 Version & Regression Information
  • This behavior is present in the current version of TypeScript, 5.4.4
⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAEBiD29oG8BQ0PQgUwC7QCMwAnACiIC8AuLXYgSwDsBzAShWgF9VvVh5GEfADNE0ALzRG2AO5xEpVgG4+AodABuYEAFdsNIQxYToo+ADoixJdAD0t6ABUAFvRiQI9ZowC22RvgQzvA6IAAmUvD4BNjQ2iDwMthhADSmiJYk0DL0ICBxIDJgAJ4wMdA6jGHYwkzJAIRAA

💻 Code
class Foo {
    set bar(baz: string) { }
}

const foo = new Foo();
const value: string = foo.bar; // This assignment should not be allowed, foo.bar will always be undefined!
🙁 Actual behavior

There is no error reported during type-checking.

🙂 Expected behavior

The type-checking should report an error for the assignment in the last line as foo.bar will always be undefined.

Additional information about the issue

#21759 is related, but that issue proposes a whole new language feature, this issue is solely about the lack of a type error.

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 and reproduce the setter-only property assignment to confirm the current type-checking behavior. Trace the type-checker path involved in reading foo.bar, then add coverage showing that assigning the read result to string reports an error without implementing the separate feature discussed in #21759.

Written by the indexing model from the issue text.

Assessment

Tech stack
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.