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

Extra arguments with defaults get implicit any even with noImplicitAny #17904

Closed
steadicat opened this issue Aug 18, 2017 · 6 comments
Closed
Labels
Bug A bug in TypeScript
Milestone

Comments

@steadicat
Copy link

TypeScript Version: 2.4.2

Code

function foo(f: (x: number) => void) {}
foo((x, arg = true) => {});

Tested in Playground with noImplicitAny on.

Expected behavior:
Type of arg is inferred to be boolean, or an error is thrown ("arg implicitly has 'any' type").

Actual behavior:
Type of arg is any, and no error is thrown.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 18, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 18, 2017

We need a contextual type before evaluating the expression, since the type of the expression can be impacted by that. we got one from the declaration of foo, it did not have a type for arg, so it was given a type any.
the fix here is either to give the callback declaration in foo a type for arg, or give arg an explicit type parameter.

@mhegazy mhegazy added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels Aug 18, 2017
@steadicat
Copy link
Author

Giving arg an explicit type works, but I wish there were a way to catch this issue when it's introduced. With noImplicitAny set, I was not expecting to have any types silently inferred.

@mhegazy mhegazy added Bug A bug in TypeScript and removed Design Limitation Constraints of the existing architecture prevent this from being fixed labels Aug 18, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 18, 2017

that is fair. an error should have been raised here. there is parallel to this case in #11905 already detected as a no-implicit-any error if the contextual type failed to be applied.

@mhegazy mhegazy added this to the TypeScript 2.6 milestone Aug 18, 2017
@falsandtru
Copy link
Contributor

@mhegazy @sandersn Dup of #17559

@mhegazy
Copy link
Contributor

mhegazy commented Sep 11, 2017

it is, thought this issue is to track raising an error under noImplicitAny, rather than changing the behavior.

@jakebailey
Copy link
Member

Just looking at old issues; given #17904 (comment) and the fact that the behavior was changed, the implication is that an implicit any is no longer the desired behavior and therefore no error should appear (so this issue can be closed).

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

No branches or pull requests

6 participants