Microsoft/TypeScript

Elementwise elaboration fails to be found for properties mixed with incompatible index signatures using intersections

Open

#58,344 建立於 2024年4月28日

在 GitHub 查看
 (0 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
Domain: IntersectionHelp WantedPossible Improvement

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

🔎 Search Terms

elementwise elaboration errors write type index signature intersection

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.0-dev.20240428#code/MYewdgzgLgBFCm0BcMDeMDaBrF0BOAlmAOYC6KYArgLYBG8eMAvjAGRowBmIIuUhJZjAC8HbrxgAiWgEM8k5gG4AUEA

💻 Code

const test: { [k: string]: number } & { foo: string } = { foo: "bar" };

🙁 Actual behavior

test variable gets highlighted

🙂 Expected behavior

I'd expect foo property in the source to get highlighted

Additional information about the issue

I don't really expect this to get any meaningful priority ;p it's just something interesting that I noticed while investigating some legitimate question as to why an error is not on the property itself in a more complex situation. Such intersections are flawed on principle so 🤷‍♂️

This happens because elaborateElementwise uses getBestMatchIndexedAccessTypeOrUndefined and that prefers concrete properties on intersections over index signatures. The problem is that you can read such dubious properties from the type (and it has a matching type in the example above so it doesn't become an elementwise candidate) but you can't quite write to them.

貢獻者指南