Typescript: Agregue Notification.requestPermission ()

Creado en 11 may. 2015  ·  19Comentarios  ·  Fuente: microsoft/TypeScript

Comentario más útil

Por cierto, hice una definición actualizada para 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[]
}

Todos 19 comentarios

Tratamos de no poner cosas no estandarizadas en lib.d.ts.

Siempre puede incluir un archivo .d.ts usted mismo que incluya definiciones para estas API.

¿No es la API de notificaciones realmente un estándar? https://notifications.spec.whatwg.org/

Dado que la API de notificación es parte de Web Workers, esperaría tenerla definida en una de las bibliotecas estándar (por ejemplo, con la nueva opción TS 1.9 --lib ).

Esto se cerró hace más de 13 meses, cuando Notification era estándar.

Esto se puede solucionar contribuyendo al TS-Lib-Generator .

Gracias @kitsonk . addedTypes.json (como indican las pautas de contribución), ya que La interfaz tiene propiedades estáticas / de solo lectura que no cubre el script de compilación.

Esta es la declaración necesaria:

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

Estoy un poco sorprendido de que aún no haya llegado, aunque no he mirado al maestro. Está en Edge 14, por lo que los IDL para Edge deberían reflejar eso, lo que debería generarlo sin anulaciones ... No sé lo suficiente, sobre los detalles, pero ¿aparecen nuevas API automágicamente cuando se agregan a los IDL de Edge y qué versión de los IDL se utilizan al construir (solo se libera)?

Ah, ya veo, los IDL del navegador tienen actualmente 4 meses ... lo que significa que es probable que sean de "Edge 13". @zhengbli ¿con qué frecuencia / cuándo los actualiza?

Solíamos llevar los archivos de especificaciones de Edge junto con las versiones oficiales de Windows 10, por lo que la última actualización fue la misma que la de TH2. Sin embargo, ahora tiene más sentido hacer una actualización más frecuente para evitar esfuerzos en vano por parte de la comunidad, estamos llevando al equipo de Edge tratando de hacer una actualización mensual. Además, NotificationOptions está disponible en la última versión de Edge. Debería cubrirse la próxima actualización, que debería ser pronto.

@zhengbli ¿Hay alguna actualización sobre este tema?

Por cierto, hice una definición actualizada para 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[]
}

¡Buen trabajo! Lo mejoré un poco: faltaba el método 'cerrar' en la clase de notificación y la definición del método requestPermission obsoleto no era del todo correcta.

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

¿Cómo observa un clic de acción?

@ mwent-cray StackOverflow es el lugar para preguntas.

@marcovdb @mhegazy ¿Las definiciones de notificación todavía están en TS 2.4? Porque parece que no puedo encontrarlo allí: - |

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

@marcovdb, ¿hay alguna razón por la que la interfaz sea diferente del código mencionado en su última publicación en el hilo?

Lo siento, no tengo idea. Tendría que preguntarle a la persona que realmente contribuyó con las definiciones. Ese sería @zhengbli.

@zhengbli ¿ omitieron todos menos 5 de los NotificationOptions ? 😞

@ialexryan, por favor, presente un nuevo problema y dénos un poco de contexto sobre el problema, y ​​estaré encantado de investigarlo.

¿Fue útil esta página
0 / 5 - 0 calificaciones

Temas relacionados

MartynasZilinskas picture MartynasZilinskas  ·  3Comentarios

fwanicka picture fwanicka  ·  3Comentarios

siddjain picture siddjain  ·  3Comentarios

Roam-Cooper picture Roam-Cooper  ·  3Comentarios

manekinekko picture manekinekko  ·  3Comentarios