-
Notifications
You must be signed in to change notification settings - Fork 64
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
propIs #147
propIs #147
Conversation
Fair question. Thank you for asking. Short version, check type guards; just saying
TL;DR: I'd been trying to under all circumstances properly infer return types with maximum accuracy, which is hard, while doc types just aim to explain to humans. My reasoning is docs can take care of the explaining, while only the typings could help bring inference, so I'd say let each do what they do best. If they could be readable too, that'd be wonderful, but I'd consider it secondary -- and just that goal of inference has been challenging enough by itself. Right now I just wanna make our tests pass. That still seems hard enough as-is. If you'd like, I actually have an even stronger example for you: consider the implementation of And we might be a bit exceptional in this respect -- you'll notice that Lodash's TS typings haven't been made remotely as complex. They're much closer to the doc-style A short example of my view: what is the type of The thing is, you may then navigate to the Why say
On this particular one, TS's type guard section provides some background.
You noted
You're not wrong. In fact, type definitions have no effect at run-time whatsoever -- everything is run by Ramda itself. So the role of these typings isn't so much guiding Ramda at run-time, but rather guiding the user such as to write correct sane programs, telling them the result type, and calling out whenever the user has done something wrong in any chain of logic containing Ramda calls.
Right. Ramda will not error on this, just returning My reasoning here has been not so much what Ramda has been doing, but rather what would be the most useful response here. In this particular case, we can see the value would never contain the checked property. In that case, I would wonder whether this call is useful. I'd be inclined to conclude the user has been making a mistake. You may have a broader view on this, and I'm curious about counter-examples. Does that line of reasoning resonate with you? Have you found cases where this errored for you in a context you did consider the call useful? So, how did they end up this complex?There's been a couple things:
You may wonder: might it be nicer to have TS typings optimized for instructing users? Yeah. But I'd rather have it not detract from this first goal of just allowing users to write code and be told the result type when right, and get an error if wrong. I'm sorry this ended up long, and I hope you'll sympathize on these considerations. That said, if there is demand for a version of the typings aimed at humans rather than inference, I'd be open to the idea of having a branch for that. |
The context I'm using ramda in is for example ngrx/store reducer functions to handle Action={type:string, payload?:any} objects. These payloads are not explicitly typed. I understand your urge to have complete type inference and I would appreciate it as well, but I still don't agree in this case. Should a declaration file not describe the javascript library (as is) so it can be used in typescript code, and not add additional restrictions for what is clearly legal behaviour for this library. When i try: |
Yeah, ngrx reducers functions are one of the use-cases most important to me personally as well. I managed to reproduce your issue, and pushed a fix to address it now -- not throwing out the type inference, but rather just allowing fallbacks to catch cases like this. The error was supposed to only trigger when it's actually known the key doesn't exist, but seems you're getting Your glitch affected any functions using Thanks for reporting this. Definitely let me know in case you run into similar issues. |
I don't understand why all these definitions have been made so complex.
Let's take propIs as an example:
Looking at the ramda source code for propIs (https://github.com/ramda/ramda/blob/v0.23.0/src/propIs.js), the signature is defined as "
Type -> String -> Object -> Boolean
".Why the generics? Why the Record? Why not simply return boolean? By making it so complex, restrictions and logic are added which the actual ramda library should be able to handle itself.
R.propIs(Number, 'x', {});
is a valid call returnning false, no error should be expected (https://github.com/ramda/ramda/blob/v0.23.0/test/propIs.js).Maybe I see it too simple, but what is currently in this definition-file is way to complex.