What is the reason behind the callback assuming the type string | number | boolean
instead of determining the exact type based on the property passed as the first argument in the carWithListener.on
function?
const car = {
paint: "red",
tires: "mrf",
brakes: "disk",
bluetooth: true,
mileage: 7.45
}
type PropChangeListener<T> = {
on<Key extends string & keyof T>(
prop: `on${Capitalize<Key>}Changed`,
callback: (val: T[Key]) => T[Key]
): void
}
declare function registerForChanges<T>(obj: T): PropChangeListener<T>
const carWithListener = registerForChanges(car);
carWithListener.on("onBluetoothChanged", (val: boolean) => !val)