I'm trying to enforce to pass an object with properties existing in a derived class in the following way:
class Observable<Event> { // A simple implementation of the observer pattern
notify( p: Event ) {
this.callback(p)
}
callback: ( p: Event ) => void
}
// the event will be an object with keys on the Observable class
type ChangeEvent<T> = {
[ P in keyof T ]?: T[P]
}
class Base {
changeAndNotify<P extends keyof this>( key: P, value: this[P]) {
this[key] = value
this.observable.notify({ [key]: value }) // this works
}
observable: Observable<ChangeEvent<Base>> // I tried this instead of Base but does not work
}
class Derived extends Base {
doSomething() {
this.changeAndNotify( 'name', 'Jane' ) // this works
this.observable.notify({ name: 'Jane' }) // this produces an error
}
name: string
}
I want the Derived class properties to be accepted by the notify
method but only properties from Base class ara valid.
Any sugestion?