#Generics issue

22 messages · Page 1 of 1 (latest)

fading river
#

So I am trying to make it so bind event receives an object of type BoundEventTarget<ObjectType,ObjectEventsType> and allow the function to infer the event names and their corresponding callbacks. that part works and while I can use "on" and "off" once I have filled in "ObjectType" it does not work when I try to use "on" inside the "bindEvent" function.
Here is the code

honest basinBOT
#
Tare#3664

Preview:```ts
export type Awaitable<T> = T | Promise<T>

export type BoundEventCallback<
T extends
| ((...args: any[]) => Awaitable<void>)
| any[]

= T extends any[]
? (...args: T) => Awaitable<void>
: T

export type TargetEvents = {
[
key: string | symbol | number
]: BoundEventCallback<any
...```

fading river
#

Generics issue

honest basinBOT
#
angryzor#9490

Preview:```ts
export type Awaitable<T> = T | Promise<T>

export type BoundEventCallback<
T extends
| ((...args: any[]) => Awaitable<void>)
| any[]

= T extends any[]
? (...args: T) => Awaitable<void>
: T

export type TargetEvents = {
[
key: string | symbol | number
]: BoundEventCallback<any
...```

carmine merlin
#

It's an any issue

#

@fading river

fading river
#

here ?

carmine merlin
#

yes

fading river
#

ah

#

then what can I replace it with

carmine merlin
#

I replaced it with {}

fading river
#

hm

carmine merlin
#

which is more correct I think?

#

In any case don't use any, use unknown and never instead

carmine merlin
#

any is really only meant to deal with completely untyped JS code

fading river
#

oh

#

then i have a lot of refactoring to do

carmine merlin
#

Well, as long as it's in constraints it won't harm your codebase if it's already working

#

But it's good practice to avoid it