IsaacScript / isaacscript

IsaacScript is a tool to help you create Binding of Isaac: Repentance mods using TypeScript.

Home Page:https://isaacscript.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

There's no way to extend ModCallback enum for custom typed callbacks.

aleksander-ciesielski opened this issue · comments

Isaac API supports custom callbacks, although in IsaacScript there's no way to extend it as it's tied to ModCallback enum, which isn't extensible in TypeScript:

AddCallback<T extends ModCallback | string>(
  modCallback: T,
  ...args: T extends ModCallback ? AddCallbackParameters[T] : unknown[]
): void;

Declaring a new overload for AddCallback isn't an option either, as the base signature will always be picked over the new one. A way to fix this issue is to change the guard of T to keyof AddCallbackParameters, which would allow adding custom callbacks by augmenting AddCallbackParameters interface:

declare global {
  interface AddCallbackParameters {
    MY_CUSTOM_CALLBACK: [(param: boolean) => string];
  }
}

Yeah, but you can't extend the imported enum - you can only make it work on compile-time by extending the declaration. Changing the guard would allow string-based callbacks to be strongly typed (AddCallback already accepts strings as events but with unknown[] as arguments).