类型不可分配 (TypeScript 4.1.2)

Type not asignable (TypeScript 4.1.2)

我正在使用旧项目 (typescript 2) 中的这段代码,但它不适用于 typescript 4.1.2。我正在尝试处理来自电子应用程序(渲染器进程)的桌面通知上的点击事件:

const nativeNotification = window.Notification;
const ProxyNotification = (title: any, options: any) => {
  const mirrorNotification = new nativeNotification(title, options);
  mirrorNotification.onclick = () => {
     // Handle click event.
  };
};
ProxyNotification.permission = nativeNotification.permission;
ProxyNotification.requestPermission = nativeNotification.requestPermission;
window.Notification = ProxyNotification;

最后一行抛出这个错误:

Type '{ (title: string, options?: NotificationOptions): void; permission: NotificationPermission; maxActions: number; prototype: Notification; requestPermission: (deprecatedCallback?: NotificationPermissionCallback) => Promise<...>; }' is not assignable to type '{ new (title: string, options?: NotificationOptions): Notification; prototype: Notification; readonly maxActions: number; readonly permission: NotificationPermission; requestPermission(deprecatedCallback?: NotificationPermissionCallback): Promise<...>; }'. Type '{ (title: string, options?: NotificationOptions): void; permission: NotificationPermission; maxActions: number; prototype: Notification; requestPermission: (deprecatedCallback?: NotificationPermissionCallback) => Promise<...>; }' provides no match for the signature 'new (title: string, options?: NotificationOptions): Notification'.

如果能帮我解决这个问题,我将不胜感激。

亲切的问候。

我假设,您想扩展 Native window.Notification。 以下是我的做法:


const nativeNotification = window.Notification;

class ProxyNotification extends Notification {
        constructor(title: string, options?: NotificationOptions | undefined) {
                super(title, options)
        }
        customMethod() {
                console.log('hello')
        }
        /**
         * Here you can define your own methods and properties
         */
}

window.Notification = ProxyNotification