Add well-known symbols for ReadonlyMap and ReadonlySet#60142
Add well-known symbols for ReadonlyMap and ReadonlySet#60142milkcask wants to merge 3 commits intomicrosoft:mainfrom
Conversation
|
@typescript-bot test it |
|
@jakebailey Here are the results of running the user tests with tsc comparing Everything looks good! |
|
Hey @jakebailey, the results of running the DT tests are ready. Everything looks the same! |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Something interesting changed - please have a look. Details
|
|
I'm not clear on why this is needed?
|
|
Closing per #60042 (comment) |
There was a problem hiding this comment.
Pull request overview
This PR fixes a lib.d.ts gap so ReadonlyMap and ReadonlySet include the well-known symbol member [Symbol.toStringTag], aligning them with Map/Set and addressing the keyof/Exclude<> behavior reported in #60042.
Changes:
- Add
readonly [Symbol.toStringTag]: stringtoReadonlyMapandReadonlySetinlib.es2015.symbol.wellknown.d.ts. - Add a new conformance test that indexes
[Symbol.toStringTag]onReadonlyMap/ReadonlySet(and related built-ins). - Add the corresponding reference baselines (
.types,.symbols,.js) for the new test.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/lib/es2015.symbol.wellknown.d.ts |
Adds [Symbol.toStringTag] to ReadonlyMap/ReadonlySet via interface merging. |
tests/cases/conformance/es6/Symbols/symbolWellKnown.ts |
New conformance test exercising [Symbol.toStringTag] on ReadonlyMap/ReadonlySet and other types. |
tests/baselines/reference/symbolWellKnown.types |
Baseline for inferred/checked types from the new test. |
tests/baselines/reference/symbolWellKnown.symbols |
Baseline for symbol resolution from the new test. |
tests/baselines/reference/symbolWellKnown.js |
Baseline for JS emit from the new test. |
| var set = new Set(); | ||
| set[Symbol.toStringTag]; | ||
|
|
||
| var readonlySet = <ReadonlySet<any>>new Set(); |
There was a problem hiding this comment.
readonlySet is asserted as ReadonlySet<any> while the rest of the file mostly uses unknown, and both readonlyMap/readonlySet use angle-bracket assertions. Using a variable type annotation (and keeping unknown consistent) would avoid any in the baseline and make the intent clearer.
| var readonlySet = <ReadonlySet<any>>new Set(); | |
| var readonlySet: ReadonlySet<unknown> = new Set(); |
| var readonlyMap = <ReadonlyMap<unknown, unknown>>new Map(); | ||
| readonlyMap[Symbol.toStringTag]; | ||
|
|
||
| var weakMap = new WeakMap(); | ||
| weakMap[Symbol.toStringTag]; | ||
|
|
||
| var set = new Set(); | ||
| set[Symbol.toStringTag]; | ||
|
|
||
| var readonlySet = <ReadonlySet<any>>new Set(); | ||
| readonlySet[Symbol.toStringTag]; | ||
|
|
There was a problem hiding this comment.
The new test verifies that [Symbol.toStringTag] is indexable on ReadonlyMap/ReadonlySet, but it doesn't directly cover the original reported regression scenario where Symbol.toStringTag was unexpectedly included in Exclude<keyof Map, keyof ReadonlyMap> / Exclude<keyof Set, keyof ReadonlySet>. Consider adding type-level assertions (e.g. type ... = Exclude<...> plus // @ts-expect-error assignments using Symbol.toStringTag) so the test fails if Symbol.toStringTag ever drops out of keyof ReadonlyMap/ReadonlySet again.
Fixes #60042
I chose not to use strict string literals or exact well-known tags like
MapandSetfor the same reason mentioned in #19006. Instead, I opted to usestring. Contra #59417.