Conflicting definitions for Set constructor causes unexpected default generic in TS 3.5
See original GitHub issueTypeScript Version: 3.4.5 vs versions after 3.5
Search Terms: set constructor setconstructor unknown
Code
let s = new Set();
Expected behavior:
In TS 3.4: we expect the default chosen param to give us a Set<{}>
.
In TS 3.5: we expect a Set<unknown>
.
That change was an intended change in TS 3.5.
Or, if Set has a default generic arg, I expect the same default chosen in 3.4 and 3.5.
Actual behavior:
In TS 3.4, this actually was a Set<any>
!
It appears there are multiple overloads of the Set constructor.
lib.es2015.collection.d.ts says:
new <T = any>(values?: ReadonlyArray<T> | null): Set<T>;
while lib.es2105.iterable.d.ts says:
new <T>(iterable?: Iterable<T> | null): Set<T>;
Note that one has T = any
and the other doesn’t.
So somehow TS 3.4 vs TS 3.5 decided to change whether to obey the any
default, I think?
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Constructor of a generic class cannot be assigned to ... - GitHub
IIUC, the compiler is not able to treat DefaultCrudRepository as a generic constructor, instead if specializes the constructor function too ...
Read more >Documentation - Classes - TypeScript
Constructors can't have return type annotations - the class instance type is always what's returned. Super Calls. Just as in JavaScript, if you...
Read more >Groovy Language Documentation
Default conflict resolution; User conflict resolution. Runtime implementation of traits ... List , as Groovy doesn't define its own collection classes.
Read more >Google C++ Style Guide
The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code ....
Read more >Changelog - Cypress Documentation
Fixed a regression introduced in the Electron browser in Cypress 10.8.0 where the CYPRESS_EVERY_NTH_FRAME environment variable was not being set appropriately ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Circling back on this – I think maybe I failed to communicate the bug here.
In practice, TS3.4 => TS3.5 changed what
new Set()
gives you, fromSet<any>
toSet<unknown>
. (That is too late to undo, though massively annoying for us to upgrade through.)But the actual bug here is that I think you didn’t change the definition of Set! It still says
any
in the typings, something else must be going wrong.Just to restate, what I am concerned about here is whether “new Set()” is supposed to give you a
Set<any>
or aSet<unknown>
, given the conflicting definitions in the typings.I believe the intention was for 3.5 change it to
<unknown>
and for that to break apps – that is at least what 3.5 did in practice – but I think the resolution of this bug is to make the typings express that intent more clearly.