#Create type by extracting parameters of other type

6 messages · Page 1 of 1 (latest)

toxic pebble
#

I want to create a new type, let's say EventNames to be an union of strings. This type would be defined by extracting the event names from the EventListeners type, defined as:

export type EventListeners<T> = {
  (event: 'debug', listener: (message: string) => void): T
  (event: 'raw', listener: (nodeId: string, payload: unknown) => void): T
  (event: 'pong', listener: (nodeId: string, ping?: number) => void): T
}

Basically, in this case, I wanted to have EventNames to be 'debug' | 'raw' | 'pong'.

Is this achievable?

summer field
#

@toxic pebble I don't think it is - type utilities like Parameters don't work on oerloads.

#

But you can define this a different way where it'd work, I think.

toxic pebble
#

Unfortunately, that's how it's defined in a library that I am using. I've managed to achieve this but instead of a type I had an interface with a slightly different structure, so I guess I would need to ask the developer directly to type it differently

#

Thanks anyways!

summer field
#

Yeah, not much you can do then.