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

any type in callback parameter #1584

Closed
NN--- opened this issue Jan 1, 2015 · 10 comments
Closed

any type in callback parameter #1584

NN--- opened this issue Jan 1, 2015 · 10 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@NN---
Copy link

NN--- commented Jan 1, 2015

Is it intended behavior ?

function x(f: (g: number) => void): void { f(1); }

x(function (d) {
   // d is number
});

Adding parentheses around the function makes 'd' to be any type:

function x(f: (g: number) => void): void { f(1); }

x((function (d) {
  // d is any
}));
@DanielRosenwasser
Copy link
Member

Yes; parentheses "turns off" contextual typing.

It is definitely something we're aware tends to be a problem for many, and #920 moves to give you the behavior you expect.

@DanielRosenwasser DanielRosenwasser added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jan 1, 2015
@NN---
Copy link
Author

NN--- commented Jan 1, 2015

The problem is that I have code like this:

f((function(d){
}).bind(abc));

I do need parentheses here and then I lose type safety:(

@CyrusNajmabadi
Copy link
Contributor

@nn there is nothing we can do in that case. Contextual typing wouldn't apply anyways as the expression being passed to 'f' is the '.bind' call, not the function expression.

@NN---
Copy link
Author

NN--- commented Jan 1, 2015

I see but in the function itself I expect parameter "d" to be a number and get an appropriate code completion.
The real case is some class and it is more complicated.

Also there is a workaround specifying type explicitly :

x((function (d:number){
}).bind(abc));

I really would like to make the compiler inferring the type instead of writing by myself.

Thanks .

@DanielRosenwasser
Copy link
Member

I see - so you want contextual typing to apply simply for the sake of editing prior to the call to bind. That's an interesting scenario, but keep in mind that even with the relevant change, if you ever went back to change your function, your parameter would implicitly be typed as any.

@NN---
Copy link
Author

NN--- commented Jan 2, 2015

It is pretty annoying but I don't know how to solve it .
I think VS integration could propose a type hint in this case. I mean it can show a popup suggesting adding explicit types to callback function parameters until there will be a perfect solution.

@DanielRosenwasser
Copy link
Member

As a note, compiling with the --noImplicitAny flag will appropriately hint that off to you.

@NN---
Copy link
Author

NN--- commented Jan 15, 2015

Thanks.
I didn't notice that it is off by default.

In any case I would like to see some solution for this.
It is very common in JS to use 'bind' function and losing type safety is not a good option.

@danquirk
Copy link
Member

See #212

@DanielRosenwasser
Copy link
Member

Additionally, we have contextual typing on parenthesized expressions in master now.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

4 participants