Skip to content

more sound typing using tuples instead of overloads - #57

Merged
staltz merged 2 commits into
callbag:masterfrom
niieani:tuple-types
Aug 25, 2021
Merged

staltz merged 2 commits into
callbag:masterfrom
niieani:tuple-types

Conversation

@niieani

@niieani niieani commented Aug 16, 2021

Copy link
Copy Markdown
Contributor

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 🤦

@staltz staltz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

I'll still ask for others to take a look as well: @jvanbruegge @Andarist

Comment thread types.d.ts
// error:
| [type: END, error: unknown]
// end without error:
| [type: END, _?: undefined]

@jvanbruegge jvanbruegge Aug 16, 2021 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the two error/end cases not one?

@niieani niieani Aug 17, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.:

Screen Shot 2021-08-16 at 6 35 42 PM

Screen Shot 2021-08-16 at 6 35 02 PM

@Andarist

Copy link
Copy Markdown
Contributor

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 args is not a conventional way of declaring those functions and using a more classic (type, data) => {} doesn't allow this to be assigned to Source and SInk types:
TS playground

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.

@niieani

niieani commented Aug 18, 2021 •

Copy link
Copy Markdown
Contributor Author

@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!

@Andarist

Copy link
Copy Markdown
Contributor

@Andarist they are assignable, you just need to make the second argument optional (it wasn't in your playground)

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.

@niieani

niieani commented Aug 22, 2021

Copy link
Copy Markdown
Contributor Author

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?
🙇

@staltz

staltz commented Aug 25, 2021

Copy link
Copy Markdown
Member

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.

@staltz
staltz merged commit 91665d5 into callbag:master Aug 25, 2021
@teohhanhui

Copy link
Copy Markdown

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.

@Andarist

Copy link
Copy Markdown
Contributor

Btw. this will improve how the tuple-based parameters like this will behave in the function's body when used in a natural way:
microsoft/TypeScript#47190

@niieani

niieani commented Dec 27, 2021

Copy link
Copy Markdown
Contributor Author

@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.
For example, if we want to properly support converting generator function instances to callbags, we'd have to be able to pass in data using the value parameter iterator.next(value).

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.
Do you have a code example to make that easier?

@teohhanhui

Copy link
Copy Markdown

@niieani:

Do you have a code example to make that easier?

This is something I discovered while working on a Rust port: https://github.com/teohhanhui/callbag-rs

Specifically the concat operator where the lastPull is saved (this might serve as your example?):

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

⌂ Proxy