Skip to content

Implicit anys with Map even if noImplicitAny is enabled #35259

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

Closed
Daniel15 opened this issue Nov 21, 2019 · 5 comments
Closed

Implicit anys with Map even if noImplicitAny is enabled #35259

Daniel15 opened this issue Nov 21, 2019 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@Daniel15
Copy link

Daniel15 commented Nov 21, 2019

TypeScript Version: vNightly in Playground

Search Terms:
inferred any, implicit any, unsound

Code

function getStrings(): Map<string, string> {
  const val = new Map();
  val.set(42, true);
  return val;
}

Expected behavior:
I expected this to throw an error if noImplicitAny is enabled.

For what it's worth, Flow handles this correctly.

Actual behavior:
No errors

Playground link

Related Issues:
No

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug Duplicate An existing issue was already created and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Nov 21, 2019
@RyanCavanaugh
Copy link
Member

Duplicate #33279

Search term: noImplicitAny Map

@Daniel15
Copy link
Author

Daniel15 commented Nov 21, 2019

@RyanCavanaugh So is this actually expected behaviour? This seems like a big hole in TypeScript's typing.

Flow gets this right.

@krisdages
Copy link

krisdages commented Nov 22, 2019

I've also been burned by this kind of thing before; usually when RXJS pipe inference gets widened to any without my realizing it.

Even if noImplicitAny is working as expected,

A stricter compile option that would force assertions before passing or assigning something with an inferred any Type Parameter to an explicitly narrower type would be immensely helpful.

This example with new Map() is relatively simple, but in more complex inference scenarios (e.g rxjs pipe chaining or third party definitions whose generics and defaults you don't necessarily know), it's difficult to tell when you have lost type safety, but the error potential is huge.


With an option like I am imagining, in the OP's example there would be no error in creating the Map or setting the values, but it would be an error to return it as Map<string, string> unless the constructor had explicitly been called with <any, any>

@krisdages
Copy link

krisdages commented Nov 22, 2019

Another source of these unexpected "any" bugs is when a conditional type resolves to "any", often unexpectedly.

I have looked into things like dtslint to try and catch these, but they have been sort of a square peg / round hole situation.

If there are any alternatives to a compiler option that could alert me when any is being used when the declaration says something else, I would love to find out about them. (Sincerity quotes, no sarcasm)

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants