#Typing callback arguments

1 messages · Page 1 of 1 (latest)

clear kite
#

I'm using a framework (Phaser) where I tell the framework to call my callback like:

this.time.addEvent({
  callback: doTick,
  args: [this.map]
})

I type my function like:

const doTick = (map: Phaser.Tilemaps.Tilemap) => { ... }

Is there some way to unify the definitions of function arguments across these two snippets, so that Typescript ensures the caller and callback argument list is in sync?

#

Typing callback arguments

hollow vigil
#

you can extract the Parameter definition with Parameters<Function> I tried to build something for you 🙂

digital charmBOT
#
sysix_#0

Preview:```ts
const event1 = (arg1: string, arg2: number) => {}

const event2 = (arg1: object, arg2: string) => {}

function addEvent<
T extends (...callbackArgs: Parameters<T>) => void

(args: {callback: T; args: Parameters<T>}) {
console.log(typeof args.callback, typeof args.args)
...```

clear kite
#

@hollow vigil it took a while for me to get back to you, but thanks, I'm using your solution now 😊