8000 Overly verbose typing · Issue #2 · billba/prague · GitHub
[go: up one dir, main page]

Skip to content
Overly verbose typing #2
@billba

Description

@billba

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0