Typescript Events

29 views
Skip to first unread message

Atanas Bozhkov

unread,
Nov 9, 2018, 5:07:54 AM11/9/18
to excaliburjs
Hello, 

I'm new to ExcaliburJS and game development, but I find the project very interesting. 

I was looking at the type definitions for the Event system - specifically the ones defined in Events.ts - https://github.com/excaliburjs/Excalibur/blob/master/src/engine/Events.ts
All of them seem to be exported as types which makes them unusable as values (correct me if I'm wrong). 
I found myself defining an enum in my own project so that I don't have to rely on magic strings.

Something in the lines of:
export enum Events {
  KILL = 'kill',
  PREKILL
= 'prekill',
  POSTKILL
= 'poskill'
 
// etc..
}

so that then you can simply use like so:
myActort.on(ex.Events.KILL, event => onActorKillHandler);

Is this currently possible or am I missing something obvious? If it's not there I'll be happy to implement it and raise a PR. 

Thanks, 
Nasco

Erik Onarheim

unread,
Nov 9, 2018, 8:46:20 AM11/9/18
to Atanas Bozhkov, excaliburjs
Hi Nasco,

You are correct, you unfortunately cannot use the exported types as values in typescript. We are very interested in a PR for non-magic strings!

The types do flow correctly with the magic strings because of the string literal overloads

image.png

image.png

As far as the PR goes, lets do something like this with an enum called EventTypes following pascal casing for the individual types. Using the same string literal value in the enum still allows the types to flow for built in events while allowing others to still define there own "string" based events

image.png

That way the type signature in the final implementation would look like this when exported out of the Events namespace in Excalibur:

`ex.Events.EventTypes.PostUpdate` 

image.png

Thanks,
Erik @ Excalibur

--
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...@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/f74d917b-86f4-4868-9a48-5fe27a7b1353%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages