Type hinting for event aruments

17 views
Skip to first unread message

xorik

unread,
May 4, 2019, 5:02:09 AM5/4/19
to excaliburjs


Hi.

I'm trying to understand correct way to hint types for events. Now only this is working for me:

const player = new ex.Actor(0, 0, 30, 30, ex.Color.White)

player
.on('preupdate', (event: any) => {
 
const evt: ex.Events.PreUpdateEvent = event
})

When I try to use correct type, tsc throws a error:
player.on('preupdate', (event: ex.Events.PreUpdateEvent) => {
 
// logic
})
// Argument of type '(event: PreUpdateEvent) => void' is not assignable to parameter of type '(event?: GameEvent<any> | undefined) => void

Is there way to avoid the "any" type and the second variable?

Thanks

Erik Onarheim

unread,
May 5, 2019, 9:54:22 AM5/5/19
to excal...@googlegroups.com, xor...@gmail.com
Hi Xorik,

Feels like a bug, this seems to be a factor of typescript strict function type and strict null checking. Excalibur as a project pre-dates this typescript compiler feature, in fact we probably would have noticed this when we implemented this https://github.com/excaliburjs/Excalibur/issues/1143.

typeerror.PNG



As a temporary workaround, disabling this check in the tsconfig.json removes the error, however this is definitely unideal if contravariant functions and strict null check types are important to you.

typeworkaround.PNG



One other workaround to capture the appropriate type due to strict null checks in the handler, and add a typeguard.

type.PNG



I've opened an issue in excalibur to address this issue specifically

Thanks for asking! We'll get this sorted.
Erik 


--
You received this message because you are subscribed to the Google Groups "excaliburjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to excaliburjs+unsubscribe@googlegroups.com.
To post to this group, send email to excal...@googlegroups.com.
Visit this group at https://groups.google.com/group/excaliburjs.
To view this discussion on the web visit https://groups.google.com/d/msgid/excaliburjs/7816342c-478f-4569-ac88-2872f5fac876%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages