Typescript: 请添加Notification.requestPermission()

创建于 2015-05-11  ·  19评论  ·  资料来源: microsoft/TypeScript

最有用的评论

顺便说一句,我为Notification做了一个更新的定义:

type NotificationPermission = "default" | "denied"| "granted";

type NotificationDirection = "auto" | "ltr" | "rtl";

interface NotificationPermissionCallback {
    (permission: NotificationPermission): void;
}

interface NotificationOptions {
    dir?: NotificationDirection;
    lang?: string;
    body?: string;
    tag?: string;
    image?: string;
    icon?: string;
    badge?: string;
    sound?: string;
    vibrate?: number | number[],
    timestamp?: number,
    renotify?: boolean;
    silent?: boolean;
    requireInteraction?: boolean;
    data?: any;
    actions?: NotificationAction[]
}

interface NotificationAction {
    action: string;
    title: string;
    icon?: string;
}

declare class Notification extends EventTarget {
    constructor(title: string, options?: NotificationOptions);

    static readonly permission: NotificationPermission;
    static requestPermission(): Promise<NotificationPermission>;
    static requestPermission(deprecatedCallback: NotificationPermission): void;

    static readonly maxActions: number;

    onclick: EventListenerOrEventListenerObject;
    onerror: EventListenerOrEventListenerObject;

    readonly title: string;
    readonly dir: NotificationDirection;
    readonly lang: string;
    readonly body: string;
    readonly tag: string;
    readonly image: string;
    readonly icon: string;
    readonly badge: string;
    readonly sound: string;
    readonly vibrate: number[];
    readonly timestamp: number;
    readonly renotify: boolean;
    readonly silent: boolean;
    readonly requireInteraction: boolean;
    readonly data: any;
    readonly actions: NotificationAction[]
}

所有19条评论

我们尽量不要将非标准化的东西放在lib.d.ts中。

您始终可以自己添加.d.ts文件,该文件包含这些API的定义。

Notification API实际上不是一个标准吗? https://notifications.spec.whatwg.org/

由于Notification API是Web Workers的一部分,因此我希望在标准库之一中对其进行定义(例如,使用新的TS 1.9 --lib选项)。

这是在13个月前关闭的,当时Notification来自标准。

这可以通过向TS-Lib-Generator做出贡献来解决。

谢谢@kitsonk,我看了TS-Lib-Generator做出的贡献,但我猜不出在addedTypes.json文件中添加Notification类型的正确方法(因为贡献准则指出),接口具有构建脚本未涵盖的静态/只读属性。

这是必需的声明:

interface NotificationOptions {
    dir?: DOMString;
    lang?: DOMString;
    body?: DOMString;
    tag?: DOMString;
    icon?: USVString;
    data?: any;
    vibrate?: number[];
    renotify?: boolean;
    silent?: boolean;
    sound?: USVString;
    noscreen?: boolean;
    sticky?: boolean;
}

interface Notification {
    readonly title: DOMString;
    readonly dir: DOMString;
    readonly lang: DOMString;
    readonly body: DOMString;
    readonly tag: DOMString;
    readonly icon: USVString;
    readonly data: any;
    readonly silent: boolean;
    readonly timestamp: DOMTimeStamp;
    readonly noscreen: boolean;
    readonly renotify: boolean;
    readonly sound: USVString;
    readonly sticky: boolean;
    readonly vibrate: number[];
    onclick: Function;
    onerror: Function;
    close(): void;
}

declare var Notification: {
    prototype: Notification;
    readonly permission: DOMString;
    new(title: string, options?: NotificationOptions): Notification;
    requestPermission(): Promise<DOMString>;
}

尽管我还没有看过师父,但它还没有解决,我感到有些惊讶。 它在Edge 14中,因此Edge的IDL应该反映出来,应该生成它而不会覆盖...我对细节没有足够的了解,但是当添加到Edge的IDL中时会自动出现新的API以及版本是什么在构建时使用IDL(仅发布)吗?

嗯,我知道,浏览器IDL目前已经使用了4个月了……这意味着它们很可能是“ Edge 13”。 @zhengbli您多久/何时更新一次?

我们以前将Edge规格文件与Windows 10官方发行版一起使用,因此最后一次更新与TH2相同。 但是,现在进行一次更频繁的更新以避免社区的浪费是更有意义的,我们将让Edge团队尝试每月进行一次更新。 此外, NotificationOptions实际上在最新的Edge版本中可用。 它应该涵盖下一次更新,该更新应该很快。

@zhengbli有关于此问题的任何更新吗?

顺便说一句,我为Notification做了一个更新的定义:

type NotificationPermission = "default" | "denied"| "granted";

type NotificationDirection = "auto" | "ltr" | "rtl";

interface NotificationPermissionCallback {
    (permission: NotificationPermission): void;
}

interface NotificationOptions {
    dir?: NotificationDirection;
    lang?: string;
    body?: string;
    tag?: string;
    image?: string;
    icon?: string;
    badge?: string;
    sound?: string;
    vibrate?: number | number[],
    timestamp?: number,
    renotify?: boolean;
    silent?: boolean;
    requireInteraction?: boolean;
    data?: any;
    actions?: NotificationAction[]
}

interface NotificationAction {
    action: string;
    title: string;
    icon?: string;
}

declare class Notification extends EventTarget {
    constructor(title: string, options?: NotificationOptions);

    static readonly permission: NotificationPermission;
    static requestPermission(): Promise<NotificationPermission>;
    static requestPermission(deprecatedCallback: NotificationPermission): void;

    static readonly maxActions: number;

    onclick: EventListenerOrEventListenerObject;
    onerror: EventListenerOrEventListenerObject;

    readonly title: string;
    readonly dir: NotificationDirection;
    readonly lang: string;
    readonly body: string;
    readonly tag: string;
    readonly image: string;
    readonly icon: string;
    readonly badge: string;
    readonly sound: string;
    readonly vibrate: number[];
    readonly timestamp: number;
    readonly renotify: boolean;
    readonly silent: boolean;
    readonly requireInteraction: boolean;
    readonly data: any;
    readonly actions: NotificationAction[]
}

做得好! 我做了一些改进:Notification类上的'close'方法丢失了,不推荐使用的requestPermission方法的定义也不是完全正确的。

type NotificationPermission = "default" | "denied" | "granted";

type NotificationDirection = "auto" | "ltr" | "rtl";

interface NotificationPermissionCallback {
    (permission: NotificationPermission): void;
}

interface NotificationOptions {
    dir?: NotificationDirection;
    lang?: string;
    body?: string;
    tag?: string;
    image?: string;
    icon?: string;
    badge?: string;
    sound?: string;
    vibrate?: number | number[],
    timestamp?: number,
    renotify?: boolean;
    silent?: boolean;
    requireInteraction?: boolean;
    data?: any;
    actions?: NotificationAction[]
}

interface NotificationAction {
    action: string;
    title: string;
    icon?: string;
}

declare class Notification extends EventTarget {
    constructor(title: string, options?: NotificationOptions);

    static readonly permission: NotificationPermission;
    static requestPermission(): Promise<NotificationPermission>;
    static requestPermission(deprecatedCallback: (permission: NotificationPermission) => void): void;

    static readonly maxActions: number;

    onclick: EventListenerOrEventListenerObject;
    onerror: EventListenerOrEventListenerObject;

    close(): void;

    readonly title: string;
    readonly dir: NotificationDirection;
    readonly lang: string;
    readonly body: string;
    readonly tag: string;
    readonly image: string;
    readonly icon: string;
    readonly badge: string;
    readonly sound: string;
    readonly vibrate: number[];
    readonly timestamp: number;
    readonly renotify: boolean;
    readonly silent: boolean;
    readonly requireInteraction: boolean;
    readonly data: any;
    readonly actions: NotificationAction[]
}

您如何看待动作点击?

@ mwent-cray StackOverflow是提出问题的地方。

@marcovdb @mhegazy通知定义是否仍在TS 2.4中? 因为我似乎无法在其中找到它:-|

https://github.com/Microsoft/TypeScript/blob/master/src/lib/dom.genic.d.ts

@marcovdb是否有一个原因,为什么该接口与您上个线程文章中提到的代码不同?

对不起,不知道您必须问实际贡献定义的人。 那就是@zhengbli。

@zhengbli为什么除了5个NotificationOptions被省略了? 😞

@ialexryan请提出一个新问题,并为我们提供有关此问题的背景信息,我很乐意对此进行调查。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

blendsdk picture blendsdk  ·  3评论

Antony-Jones picture Antony-Jones  ·  3评论

Zlatkovsky picture Zlatkovsky  ·  3评论

uber5001 picture uber5001  ·  3评论

bgrieder picture bgrieder  ·  3评论