Skip to content

better typing for dispatch arguments #12

@billba

Description

@billba

Currently you can do:

this.dispatchTo(topicInstanceName, optional_activity, dispatchArgs);

If topicInstanceName points to an instance of MyTopic, dispatchArgs should match the typing of the arguments to the onDispatch on MyTopic. But right now there is no mechanism for doing that, so both are typed as any.

This is a tallish hill to climb.

On the child side we'd need a new generic Dispatch type for DispatchScore and Topic:

export interface DispatchScore <
    Dispatch = any
> {
    dispatchArgs?: Dispatch;
    score: number;
}

export abstract class Topic <
    Start = any,
    State = any,
    Return = any,
    Constructor = any,
    Dispatch = any,
    Context extends TurnContext = TurnContext, 
> {

    ...

    async onDispatch(args?: Dispatch) {
    }

    ...
    
    async getDispatchScore (
        activity?: Activity,
    ): Promise<DispatchScore<Dispatch> | void> {
    }

    ...

}

On the parent side we can add a new signature to dispatchTo to let it take an actual class instance:

this.dispatchTo(instance_of_MyTopic, optional_activity, dispatchArgs);

We'd probably want to change things so that this signature is the only way to provide dispatchArgs, since it's the only typesafe one. That means no passing dispatchArgs to dispatchToChild. This is probably an okay limitation given how rare dispatch scoring even is.

But I'm not sure this ultimately buys us anything, because where does this mythical instance_of_MyTopic come from anyway? loadTopic which generically returns a Topic.

I don't think this can work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions