Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get serviceworker registration object which is typescript friendly?

self.addEventListener('push', function(event:any) {
  console.log('Push event!! ', event)
  
  if (event.data) {
    console.log('This push event has data: ', event.data.text());
  } else {
    console.log('This push event has no data.');
  }

  
  const promiseChain = showLocalNotification('Yolo', event.toString(), self.registration);

  event.waitUntil(promiseChain);
  
})

Here the line const promiseChain = showLocalNotification('Yolo', event.toString(), self.registration); complains about self.registration:

Property 'registration' does not exist on type 'Window & typeof globalThis'.ts(2339)

How do I resolve this? Any pointer here would be greatly appreciated.

In my tsconfig i'm already using dom lib option.

"lib": [ "es2015" , "dom"],

like image 269
sahilsk Avatar asked Dec 12 '25 13:12

sahilsk


1 Answers

Add this to the top of your file:

/// <reference lib="webworker" />
export default null
declare let self: ServiceWorkerGlobalScope
like image 117
Maurici Abad Avatar answered Dec 14 '25 09:12

Maurici Abad