samedi 2 décembre 2023

Typescript satisfies condition with Record and generics does not work

This question is related to my other one How to store "types" and instantiate them dynamically in typescript.

Now I'm trying to add generics and abstract base classes into the mix. VsCode complains about everything. And I've tried to tweak the satisfies condition, but nothing works.

Handlers records

    private handlers = {
    [types.CommandType.Click]: handlers.ClickCommandHandler,
    [types.CommandType.Navigate]: handlers.NavigateCommandHandler,
    [types.CommandType.Type]: handlers.TypeCommandHandler,
    [types.CommandType.NewTab]: handlers.NewTabCommandHandler,
    [types.CommandType.AnalyzePage]: handlers.AnalyzePageCommandHandler,
    [types.CommandType.Connect]: handlers.ConnectCommandHandler,
} satisfies Record<types.CommandType, (new <TCommad extends types.AutomationCommand, TResponse>(pageAwaiter: Awaiter, tabs: Map<number, TabSession>) => CommandHandler<TCommad, TResponse>)>;

Error:

Type of computed property's value is 'typeof ClickCommandHandler', which is not assignable to type 'new <TCommad extends AutomationCommand, TResponse>(pageAwaiter: Awaiter, tabs: Map<number, TabSession>) => CommandHandler<TCommad, TResponse>'.
  Types of construct signatures are incompatible.
    Type 'new (pageAwaiter: Awaiter, tabs: Map<number, TabSession>) => ClickCommandHandler' is not assignable to type 'new <TCommad extends AutomationCommand, TResponse>(pageAwaiter: Awaiter, tabs: Map<number, TabSession>) => CommandHandler<TCommad, TResponse>'.
      Construct signature return types 'ClickCommandHandler' and 'CommandHandler<TCommad, TResponse>' are incompatible.
        The types of 'handle' are incompatible between these types.
          Type '(command: ClickCommand) => Promise<AutomationResponse<boolean>>' is not assignable to type '(command: TCommad) => Promise<AutomationResponse<TResponse>>'.
            Types of parameters 'command' and 'command' are incompatible.
              Type 'TCommad' is not assignable to type 'ClickCommand'.
                Type 'AutomationCommand' is missing the following properties from type 'ClickCommand': tabId, selector

CommandHandler, ClickCommandHandler and TabCommandHandler:

export interface CommandHandler<TCommand extends AutomationCommand, TResponse> {
    handle(command: TCommand): Promise<AutomationResponse<TResponse>>;
}
class ClickCommandHandler extends TabCommandHandler<ClickCommand, boolean>
abstract class TabCommandHandler<TCommand extends TabAutomationCommand, TResponse> implements CommandHandler<TCommand, TResponse>

Here's the smallest reproducible Typescript Playground.

This should be simple, what am I missing?





Aucun commentaire:

Enregistrer un commentaire