类型不可分配(TypeScript 4.1.2) [英] Type not asignable (TypeScript 4.1.2)

查看:70
本文介绍了类型不可分配(TypeScript 4.1.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using this piece of code from an old project (typescript 2) but it's not working with typescript 4.1.2. I'm trying to handle click events on desktop notification from an electron app (renderer process):

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;

最后一行抛出此错误:

键入'{(标题:字符串,选项?:NotificationOptions):无效;权限:NotificationPermission;maxActions:数字;原型:通知;requestPermission:(deprecatedCallback ?:NotificationPermissionCallback)=>承诺...}'是不可分配的键入"{new(标题:字符串,选项?:NotificationOptions):通知;原型:通知;maxonlys只读:数字;只读权限:NotificationPermission;requestPermission(deprecatedCallback ?:NotificationPermissionCallback):承诺< ...> ;;}'.输入'{(标题:字符串,选项?:NotificationOptions):无效;允许:NotificationPermission;maxActions:数字;原型:通知;requestPermission:(deprecatedCallback ?:NotificationPermissionCallback)=>承诺...}'没有匹配项对于签名新"(标题:字符串,选项?:NotificationOptions):通知".

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'.

我将为您提供一些帮助,以解决该问题.

I would appreciate some help to fix it.

亲切的问候.

推荐答案

我假设您想扩展本机window.Notification.这是我的处理方式:

I assume, you want to extend Native window.Notification. Here is how I would do this:


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

这篇关于类型不可分配(TypeScript 4.1.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆