#Extending a generic with a mixin pattern

8 messages · Page 1 of 1 (latest)

royal fiber
#

Hi, I am developing a small browser editor for my application. This editor can have tools. These tools are really just event-emitters and event consumers. There are many tools that I forsee sharing some behavior so I thought a mix-in pattern would be appropriate for this. I am mostly refferring to the mix-in pattern in the Typescript documentation here.

My tools emit events, specified by a a generic type when creating them. Mixins may say "this tool can emit these events." I am having trouble getting my mixin to extend this generic. Here is a my module currently. I have commented where I am getting type-errors.


See attached, it got too long


If somebody has a different pattern that I could follow to also get this job done, let me know. For reference, I am using React. I have an editor context with the current tool and many components may reference this tool (like for drawing, display tool tips, forwarding events, etc).

#

Just to clarify.. the error I am getting is regarding the second argument to the .publish call in Tippable.ts

Diagnostics:
1. Argument of type 'ToolTipEvent' is not assignable to parameter of type 'TEventMap["tip"]'.
     'ToolTipEvent' is assignable to the constraint of type 'TEventMap["tip"]', but 'TEventMap["tip"]' could be instantiated with a different subtype of constraint 'ToolEvent'. [2345]
#

I do not have it yet, but it is important that I can check if something isTippable, so I can safely access .tip as well as .subscribe("tip", (e: ToolTipEvent) => ...);

royal fiber
#

How can I promote this? I am quite stuck.

uncut thunder
#

Making a example, e.g. on the TS Playground that reproduces the issue would help - especially a simpler one - this would be quite a bit of code for someone to dig through to try to solve the issue.

royal fiber
#

https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBAQwEYGcZQQY3pgNglFOAIQOABUIJcAecgUQDdgA7GAWQTDlBlYBMilak1YcucALxxhuUW05gAfHACWAWzC5g6sUKq44AbwBQcOGCirGCPnFyq0rYFBQAuY2fNwA2gGk1FjgAa2AATwgAMxl5cTAAXQB+DwBlYBgaAApgDwZmBS5-eIBKKRVGCFV+JQBuLwBfKWN6uvNLCD5sYH4LAFckBxQACxoA3gEiUIjovLFFJWz8mA8-ABoLBDDcCAR+XNjFIuKPCqrjOHqTS5NQSFg4GDCwYBkDZG06A4lxlkFXkSWiiasi+yiaLGAAHc4JkAHTwhBQADm7kQLDCPhKZRIZFkn0BXFqJhu4GgO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3tx24Gd6Dw8j2F+SITAa8+rBQYgHhXZr3dN34G0XjfYBVCgZ1IRYWFg9D+5J6wKPqGSsLG4S3ub-y++4jXQCgA

#

Sure, I guess I can copy it and remove most of the context to TS Playground

royal fiber
#

I think my problem is similar to that of HTML element events.. so I'll probably just look what Typescript does to define those (I think I' already close). I'll probably drop the mix-in idea for now since it's just adding complication. After I have a clearer idea on what I actually need I'll revisit some ways to remove repeated code that the mix-ins would otherwise alleviate.