Repository navigation
more sound typing using tuples instead of overloads - #57
Conversation
staltz
left a comment
There was a problem hiding this comment.
Looks good, thanks!
I'll still ask for others to take a look as well: @jvanbruegge @Andarist
| // error: | ||
| | [type: END, error: unknown] | ||
| // end without error: | ||
| | [type: END, _?: undefined] |
There was a problem hiding this comment.
Why are the two error/end cases not one?
There was a problem hiding this comment.
I guess the only difference is that this refinement will work correctly:
if (args[0] === END && args.length === 1) {
const arg1: undefined = args[1]
}Whereas if we used [type: END, error?: unknown] instead of both, then args[1] will always be unknown. I guess TypeScript could infer this from the fact that args[1] has to be undefined if the array's .length is 1, but that feature isn't there apparently.
I realize it's not a particularly useful refinement, cause you probably wanna check args[1] for presence of the error anyway (spec says it could be 2 args with the second being undefined), but I've added it there just for the sake of completeness. Plus, I like the fact that all the possible states are represented as a separate tuple. I think that might be helpful for learning, plus depending on the current refinement, if you hover over args you get the relevant names of the arguments, e.g.:
|
I agree that this is more sound but I see a problem with this. I'm unsure if it can be solved easily. Using spread with those Other than that - labeled tuple elements have only been introduced in TS 4.0: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#labeled-tuple-elements . So it's something to consider in regards to this being a breaking change or not. |
|
@Andarist they are assignable, you just need to make the second argument optional (it wasn't in your playground): Playground. Valid point on it being a breaking change though! |
Nice, I have never specified a parameter to be optional without also specifying its type. Didn't know that it was possible. It seems like this really could be inferred by TS correctly if there is an actual signature that can satisfy the assignment type. |
|
Thanks for all your reviews! @staltz Given the feedback was addressed, would you be fine with releasing this change, perhaps as 2.0 to mark the potential breaking change in the typings? |
|
Thanks @niieani for the patience. I will release this as non-breaking change because this repo is primarily concerned with versioning the README, and also TypeScript evolves fast and does breaking changes on minor version numbers. |
|
There is a slight problem here: A "pull" is supposed to support pulling with some data as a request: #37 I think the type here is not explicit enough, though I'm not familiar with TypeScript so I've no idea if this can be expressed in a better way. |
|
Btw. this will improve how the tuple-based parameters like this will behave in the function's body when used in a natural way: |
|
@teohhanhui typing for pull data was not there in the first place, I just made the typings more strict, so this is more like a feature request. I can see there might be a few use cases for it though. It would be great to make this case more explicit in the callbag spec (or at least the getting started guide). Would need to think a bit about what the signature to support this should be though. |
This is something I discovered while working on a Rust port: https://github.com/teohhanhui/callbag-rs Specifically the https://github.com/staltz/callbag-concat/blob/db3ce91a831309057e165f344a87aa1615b4774e/readme.js#L41 So in the meantime, that's not implemented in my port: https://github.com/teohhanhui/callbag-rs/blob/b66e22a1100a96c35da9ace12297608e82596731/src/concat.rs |


This PR uses tuples instead of overloads, to allow TypeScript to correctly narrow the type of the second argument based on the type in the first argument.
Verified that the variance (updated in 4a9b347) is still working as expected, see playground.
See an example of how this improves error detection. Current implementation makes all of these example errors go undetected: Playground Link
Apologies for the double PR, I didn't realize renaming branches from the Github UI would automatically close the PR 🤦