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

Feature request - Make a parameter required for callback function #20274

Closed
ORESoftware opened this issue Nov 27, 2017 · 9 comments
Closed

Feature request - Make a parameter required for callback function #20274

ORESoftware opened this issue Nov 27, 2017 · 9 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ORESoftware
Copy link

ORESoftware commented Nov 27, 2017

This video shows the problem:

https://www.useloom.com/share/1a0f1e84083049ff8f745a3a1f17ac03

basically I have this

v.run =  t => { return t.foo()};

run(() => {   
    //  this compiles
});

run(t => {   
    //  but I want to make the t parameter required!
});

the "plain" function satisfies the typings, but I want to make the parameter required, not optional. I can't really explain but it should be clear from the (rambling) video. Thanks.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 27, 2017

have no parameters is semantically equivalent to having a parameter and ignoring its value at runtime. the later is not an indication of invalid code, and so is the earlier. a canonical example here is map.. most users never specify the second or third arguments. the caller has to always provide them just the same.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 27, 2017
@ORESoftware
Copy link
Author

ORESoftware commented Nov 27, 2017

Totally disagree on this one.

Array.prototype.map = (x: Function, y?: thisArg)

it does not look like this:

Array.prototype.map = (x?: Function, y?: thisArg)

if you don't pass the first argument, you are f*cked, because a TypeError is coming your way.

it is VERY important to tell the user that the first argument is required. Omitting an argument is a failure at runtime!!

So with that said, I need to tell my users that this ok

run(function(t){

});

but that this is not ok:

run(function(){

});

how do I do that?

@mhegazy
Copy link
Contributor

mhegazy commented Nov 27, 2017

run(function(t){
   // forget to use t
});

and

run(function(){

});

are identification.. so you can tell them to make sure to use the argument in the docs i guess..

@gcnew
Copy link
Contributor

gcnew commented Nov 27, 2017

It would be more helpful if you post a snippet displaying the issue. The way I see it, the only code that needs such behaviour are functions that inspect function.length or do toString() hacks. Honestly, both of these are questionable practices.

@ORESoftware
Copy link
Author

ORESoftware commented Nov 27, 2017

@gcnew if you use

[].map()

you are going to get a TypeError because you failed to pass it a function

same with my code (I just improved the example)

if you fail to pass t, then you will get a TypeError because t.foo() will get called, and t will be undefined.

@mhegazy yes I suppose a linting tool / rule could be used to enforce that the parameter is there. I am not sure how to classify this and it's a bit confusing tho.

@ORESoftware
Copy link
Author

ORESoftware commented Nov 27, 2017

Ultimately this is the difference between arguments and parameters?

// here are arguments
t.foo(a,b,c);      // a,b,c are arguments

// here are parameters
t.foo = function(a,b,c){   // a, b, c are parameters

};

at least that's the nomenclature that I currently adhere to lol

so I want to make parameters to be forced to be in place

what I want is useful for APIs, not for library code

the user is essentially implementing foo, but I need to tell them that foo needs to take at least one parameter

@RyanCavanaugh
Copy link
Member

It's in the FAQ and this has been covered many many times before. See #17868 for a recent recitation of this debate.

@ORESoftware
Copy link
Author

ORESoftware commented Nov 28, 2017

@RyanCavanaugh re: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-with-fewer-parameters-assignable-to-functions-that-take-more-parameters

I'd like see some option we can use to make functions with fewer parameters not assignable to functions that take more parameters

is that possible? A flag in tsconfig.json? dunno

@ORESoftware
Copy link
Author

So it's working here:

screenshot 2017-12-03 16 24 50

so I wonder why in my example it wasn't working? dunno

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants