Typescript: Please add Notification.requestPermission()

Created on 11 May 2015  ·  19Comments  ·  Source: microsoft/TypeScript

Most helpful comment

By the way, I have made an updated defintion for 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[]
}

All 19 comments

We try not to put non-standardized things in lib.d.ts.

You can always include a .d.ts file yourself that includes definitions for these APIs.

Isn't the Notification API actually a standard? https://notifications.spec.whatwg.org/

Since the Notification API is part of Web Workers, I'd expect to have it defined in one of the standard libs (e.g. with the new TS 1.9 --lib option).

This was closed over 13 months ago, when Notification was from standard.

This can be addressed by contributing to the TS-Lib-Generator.

Thank you @kitsonk I had a look to the TS-Lib-Generator to contribute but I couldn't guess the correct approach to add the Notification type in the addedTypes.json file (as the contribution guidelines state), since the interface has static/readonly properties not covered by the build script.

This is the needed declaration:

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>;
}

I am a little bit surprised that it hasn't come over yet, though I haven't looked at master. It is in Edge 14, so the IDLs for Edge should reflect that, which should generate it without overrides... I don't know enough, about the specifics, but do new APIs automagically appear when added to Edge's IDLs and what version of the IDLs are used when building (is it only released)?

Ah, I see, the browser IDLs are currently 4 months old... which means it is likely they are "Edge 13" ones. @zhengbli how often/when do you update them?

We used to take the Edge spec files along with official windows 10 releases, therefore the last update was the same as TH2. However, it makes more sense now to do a more frequent update to avoid wasted efforts from the community, we are taking to the Edge team trying to make a monthly update happen. Also, NotificationOptions is indeed available in the latest Edge build. It should be covered next update, which should be soon.

@zhengbli Is there any update on this issue?

By the way, I have made an updated defintion for 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[]
}

Great work! I improved it a bit: the 'close' method on the Notification class was missing, and the definition for the deprecated requestPermission method wasn't entirely correct.

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[]
}

How do you watch for a action click?

@mwent-cray StackOverflow is the place for questions.

@marcovdb @mhegazy is Notification definitions still in TS 2.4? Because I can't seem to find it there :-|

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

@marcovdb is there a reason why the interface is different from code mentioned in your last post in thread?

Sorry, no idea. You'd have to ask the person that actually contributed the definitions. That would be @zhengbli.

@zhengbli why were all but 5 of the NotificationOptions omitted? 😞

@ialexryan please file a new issue and give us some context about the issue, and i would be happy to look into it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OliverJAsh picture OliverJAsh  ·  242Comments

born2net picture born2net  ·  150Comments

metaweta picture metaweta  ·  140Comments

rbuckton picture rbuckton  ·  139Comments

jonathandturner picture jonathandturner  ·  147Comments