简体   繁体   中英

Generic method's typing with generic type as a parameter does not work properly

Consider the following code:

class Events<T extends object> {
    raiseEvent<K extends keyof T>(key: K, value: T[K]) {
        // do something here
    }
}

interface EventsDictionary {
    somethingHappened: number
    whatever: string
}

interface AdditionalEvents {
    disconnnected: CloseEvent
}

type AddAdditional<T extends object> = Omit<T, keyof AdditionalEvents> & AdditionalEvents

const events = new Events<AddAdditional<EventsDictionary>>()
events.raiseEvent("disconnnected", new CloseEvent("..."))  // works fine

Now if we use this class in another class that is also generic like this:

class SomeOtherClass<Something extends object> {
    constructor() {
        const events = new Events<AddAdditional<Something>>()
        events.raiseEvent("disconnnected", new CloseEvent("...")) 
        //                                 ^^^^^^^^^^^^^^^^^^^^^
        // Argument of type 'CloseEvent' is not assignable to parameter of type 'Something["connnected"] & CloseEvent'.
        //  Type 'CloseEvent' is not assignable to type 'Something["connnected"]'.ts(2345)
    }
}

I am not sure how to work around this problem.

The generic parameter isn't speciefied enough, Something needs to extends the AditionnalEvents

class SomeOtherClass<Something extends AdditionalEvents> {
    constructor() {
        const events = new Events<AddAdditional<Something>>()
        events.raiseEvent("disconnnected", new CloseEvent("...")) 
    }
}

Also to me, your AddAdditional type is weird. Should it be a union like following?

class Events<T extends object> {
    raiseEvent<K extends keyof T>(key: K, value: T[K]) {
        // do something here
    }
}

interface EventsDictionary {
    somethingHappened: number
    whatever: string
}

interface AdditionalEvents {
    disconnnected: CloseEvent
}

const events = new Events<EventsDictionary & AdditionalEvents>()
events.raiseEvent("disconnnected", new CloseEvent("..."))  // works fine


class SomeOtherClass<Something extends AdditionalEvents> {
    constructor() {
        const events = new Events<Something & AdditionalEvents>()
        events.raiseEvent("disconnnected", new CloseEvent("...")) 
    }
}

Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM