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

Overly verbose typing #2

Closed
billba opened this issue May 24, 2017 · 2 comments
Closed

Overly verbose typing #2

billba opened this issue May 24, 2017 · 2 comments

Comments

@billba
Copy link
Owner

billba commented May 24, 2017

Consider this:

first(
    rule(matchRegExp(/I am (.*)/), match => match.reply(`hi, ${match.groups[1]}`)),
    ...
);

What type is match? All we really know is that it's the input type to matchRegExp plus IRegExpMatch, which is to say that matchRegExp adds groups to the input type. But as to the overall type, and thus e.g. the type of match.reply, we've got nothing, because we don't specify the input type to rule. Which, by the way, is the answer for now:

first<YourTypeHere>(
    rule<YourTypeHere>(matchRegExp(/I am (.*)/), match => match.reply(`hi, ${match.groups[1]}`)),
    ...
);

There are two things we'd like to be able to do here to make things more concise. The first is to be able to infer the type of rule by setting it in the enclosing first:

first<YourTypeHere>(
    rule(matchRegExp(/I am (.*)/), match => match.reply(`hi, ${match.groups[1]}`)), // type is not named, so infer it to be YourTypeHere
    ...
);

That this does not work is TypeScript issue #15680.

The other is to create typed shorthands for the helpers:

const f = first<YourTypeHere>, r = rule<YourTypeHere>;
f(
        r(matchRegExp(/I am (.*)/), match => match.reply(`hi, ${match.groups[1]}`)),
        ....
);

That this does not work is TypeScript issue #15877

Until one or both of these suggestions is implemented, we're just going to be a little less concise than I would prefer if you want typing, which I do.

If you leave it out then match will always be any, which is no worse than working in plain old JavaScript...

@billba
Copy link
Owner Author

billba commented May 26, 2017

Update: the key TypeScript issue has already been fixed and is currently showing up up on the Roadmap as shipping in version 2.4.

billba pushed a commit that referenced this issue May 30, 2017
@billba
Copy link
Owner Author

billba commented Aug 12, 2018

Not relevant to latest version.

@billba billba closed this as completed Aug 12, 2018
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

No branches or pull requests

1 participant