-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Type definitions for (a small subset of) underscore #1
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
Conversation
I've seen some people write "type tests" using things like |
58ea715
to
f97ad0d
Compare
Nice suggestion, @jeffmo. I've taken a crack at adding a test. If everyone's fine with this, I can make it more complete. Library declarations seem to be a particularly buggy part of Flow — I've indicated the tests which I believe should cause an error but do not. In general, dropping the
to
resulted in no error, but putting that line higher in the file did produce an error. Looks like another Flow bug. @splodingsocks what do you think of this approach? |
@danvk Thanks for doing this! I took a look into the |
Also, the Besides, I checked the runtime behavior of |
You know you've been spending too much time in JS-land if that argument about This type checks: function foo(a, b) {}
foo(1); So I guess Flow is at least consistent, if surprising. Is there a way to tell Flow to require each parameter? I don't want to insist that each parameter be non-null or non-undefined, but I would like to require that calls to Speaking of which, why is this valid?
I realize that passing additional, undeclared arguments is technically valid JavaScript, but it certainly seems like the type of mistake that Flow would be good at catching. |
Why do you want to require this behavior when no such constraint is imposed by the library itself? Calling Regardless, there isn't a way to do this today. But I should note that the constraint "neither parameter is undefined" is exactly what you want to say—the two notions (argument not provided, undefined argument provided) are completely equivalent in JS. Furthermore, and just to be 100% clear, it isn't that Flow "doesn't check" the number of passed arguments. The issue is that the function provides no constraint on the parameter type. Considering your example above, if we add a simple constraint, Flow will find an error. /* @flow */
function foo(a, b) { a * b }
foo(1);
Regarding errors for overmany arguments. As you pointed out, doing so does not cause a runtime error, so there's not much reason to restrict that. In fact, passing "too many" arguments happens pretty often. Consider an implementation of function forEach(xs, f) {
for (let i = 0; i < xs.length; i++) {
f(xs[i], i);
}
}
// is this an error? callback takes 1 arg, passed 2.
forEach([1, 2, 3], x => console.log(x)); |
Your point that this is allowed by the library is well-taken, but I don't entirely agree with it.
and for good reason—it almost certainly represents a mistake. Calling You're right that this doesn't come up when you have an implementation for the function, but that's not really an option for declaration files. |
Whether I do agree that Flow needs some way to specify that a value should be non-void. For example, what if underscore explicitly checked that the arguments were non-void and raised a runtime error? We'd need some way to convey that constraint to Flow through the declaration, and no such way exists. So, I agree that it would be useful to have some way to declare a parameter type as non-void, and I recommend we continue that discussion in an issue over in the facebook/flow repo. Even if such a capability did exist, I'm not convinced that it would be appropriate to use it here, but that's more a matter of opinion, and mine isn't any more correct than yours. To bring this discussion back to the current issue, since there isn't a way to declare the type of this function the way you would want, I think the point is somewhat moot. I might recommend that you change the wording in your comment above the test, which indicates a bug in Flow when the behavior is actually working as designed. |
One more thought, since this commit is introducing the style for tests (which is really great, btw). What do you think of changing |
I've reworded the comment to be more accurate and changed to |
FWIW, TypeScript & the DefinitelyTyped underscore declarations flag an incorrect number of arguments as an error: One arg:
Two args:
Three args:
|
I'm not sure if @splodingsocks is affiliated with Facebook, but if we don't hear from him within a few days, maybe we should fork this over to facebook/flowtyped? cc @gabelevi |
Type definitions for (a small subset of) underscore
@danvk Thanks so much for being the first to jump in! I'm on vacation, so I haven't been keeping up with things. But we're all merged now! |
@danvk @jeffmo @samwgoldman I've added you all as collaborators on this repo, so you should have push permissions now. I'll be back from my trip on Friday. |
Add support for client-side batching to generate fewer <style> tags.
chore(koa-send options): export type options
styled-components v4: Use React$ElementRef instead of HTMLElement types
Thanks for creating this repo! Let's get things rolling.
Here is my project's lame attempt to write flow declarations for underscore. We've written declarations only as we've used new functions, so it's nowhere close to complete.
Open questions:
See also facebook/flow#946, which makes this less useful than it could be.