-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReadonlySet and ReadonlyMap are not narrowed by instanceof checks #53800
Comments
But they're not. They're independent interfaces. There's nothing in the type system that links these interfaces to |
@MartinJohns Are you suggesting that a ReadonlyMap is not a Map when compiled? The expectation is that when checked with |
Technically, neither values typed as The type system can't really represent what you want. |
I see. It's something that can easily worked around in code, I figured it might be possible to make it easier to work with these interfaces in library code. |
Related issue for readonly arrays: #17002 You can get it to narrow properly for your toCollection function by excluding readonly types from the isX functions here, playground link. Which makes more sense anyway in this particular case since readonly types are not readonly at runtime. |
Always important to point out that "readonly" only means "readonly via this interface", it does not mean immutable. |
Not being a |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
I have noticed this bug on the 5.0.4 version of typescript, but I suspect this has been around since
ReadonlySet
andReadonlyMap
were introduced.ReadonlySet
andReadonlyMap
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
ReadonlySet
andReadonlyMap
are not narrowed byinstanceof Set
orinstanceof Map
conditions🙂 Expected behavior
I would expect both
ReadonlySet
andReadonlyMap
to be narrowed byinstanceof Set
orinstanceof Map
since they are just qualifiers overSet
andMap
.The text was updated successfully, but these errors were encountered: