Skip to content
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 lacking Symbol.toStringTag #60042

Open
lionel-rowe opened this issue Sep 24, 2024 · 6 comments Β· May be fixed by #60142
Open

ReadonlySet and ReadonlyMap are lacking Symbol.toStringTag #60042

lionel-rowe opened this issue Sep 24, 2024 · 6 comments Β· May be fixed by #60142
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@lionel-rowe
Copy link
Contributor

πŸ”Ž Search Terms

ReadonlySet, ReadonlyMap, Symbol.toStringTag

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about ReadonlySet, ReadonlyMap, Symbol.toStringTag

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99#code/C4TwDgpgBAsgrsAhsAlgOwOY0WGFgAWA9gCYByiAttALxQCiAHgMYA2cJEAPANYQhEAZrBxc4aHmiIB3NABoo4yTLQA+BXwHCAShEQkiaViGxgxEqbIVLLa1QChQkWAmToMAZXx5CpCtSg6JjYObk0hKC9gc2VZdShwnT0DIxAomNtVB3sAehyoAAFgAGcAWghGSGZgcoAnWqJa+1Z8KEQALhckVExTH2JyKlpIkEoAIyJWADpgIg9gWvcAFUQMXPyisoqqmoh6xubWsc74bvco-r8hwJHxyZm5heXV+yA

πŸ’» Code

type MutatingMapMethodName = Exclude<keyof Map<unknown, unknown>, keyof ReadonlyMap<unknown, unknown>>
type MutatingSetMethodName = Exclude<keyof Set<unknown>, keyof ReadonlySet<unknown>>

// @ts-expect-error
let a: MutatingMapMethodName = Symbol.toStringTag
// @ts-expect-error
let b: MutatingSetMethodName = Symbol.toStringTag

πŸ™ Actual behavior

Both @ts-expect-errors give Unused '@ts-expect-error' directive.(2578)

πŸ™‚ Expected behavior

Both @ts-expect-errors are used

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Sep 26, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 26, 2024
@lionel-rowe
Copy link
Contributor Author

Is there any reason not to simply have the Map interface extend ReadonlyMap (and same for Set and Array)?

@milkcask
Copy link

milkcask commented Oct 4, 2024

These are well known symbols of es2015.

https://262.ecma-international.org/6.0/#sec-well-known-symbols

@milkcask
Copy link

milkcask commented Oct 4, 2024

It first appears to be safer to use the exact well-known tags rather than string, as done in a recent PR #59417. Yet, I don't think it's proper after considering #19006.

@rbuckton
Copy link
Member

These interfaces do not describe actual ECMAScript constructors. Adding Symbol.toStringTag to them would be a breaking change for anyone implementing Map- or Set-like objects/classes that are currently assignment-compatible with ReadonlyMap/ReadonlySet, such as the custom maps uses in DeoptExplorer

@milkcask
Copy link

@rbuckton I don't have a strong opinion on this one; I just found it and fixed it. That said, here's a reason we might want to add Symbol.toStringTag.

ReadonlyMap is also used to provide interfaces for the read-only browser Map-like objects.

For example,

interface EventCounts extends ReadonlyMap<string, number> {}

and at runtime,

>> performance.eventCounts
-----
EventCounts { size: 1 }
​​    constructor: function EventCounts()
​​​​        Symbol(Symbol.toStringTag): "EventCounts"
​​        [...]
     [...]

It seems ReadonlyMap does describe Map and the expectation that it is readonly.

@milkcask
Copy link

Related: whatwg/webidl#357

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants