I am writing a service for my angular 2 application using TypeScript. The service uses chrome ServiceWorker to listen for push notifications (see the tutorial ). In the code (javascript), navigator is first used to see if ServiceWorker supported, then continues to register, etc., i.e.
if ('serviceWorker' in navigator) { console.log('Service Worker is supported'); navigator.serviceWorker.register('sw.js').then(function() { return navigator.serviceWorker.ready; }).then(function(reg) { console.log('Service Worker is ready :^)', reg);
I would like to do the above using TypeScript. However, the current lib.d.ts used by the TypeScript compiler (see below) does not seem to have definitions defined in the navigator for ServiceWorker or related methods such as serviceWorker.register (I assume this is chrome-specific implementation).
interface Navigator extends Object, NavigatorID, NavigatorOnLine, NavigatorContentUtils, NavigatorStorageUtils, NavigatorGeolocation, MSNavigatorDoNotTrack, MSFileSaver, NavigatorUserMedia { readonly appCodeName: string; readonly cookieEnabled: boolean; readonly language: string; readonly maxTouchPoints: number; readonly mimeTypes: MimeTypeArray; readonly msManipulationViewsEnabled: boolean; readonly msMaxTouchPoints: number; readonly msPointerEnabled: boolean; readonly plugins: PluginArray; readonly pointerEnabled: boolean; readonly webdriver: boolean; getGamepads(): Gamepad[]; javaEnabled(): boolean; msLaunchUri(uri: string, successCallback?: MSLaunchUriCallback, noHandlerCallback?: MSLaunchUriCallback): void; requestMediaKeySystemAccess(keySystem: string, supportedConfigurations: MediaKeySystemConfiguration[]): PromiseLike<MediaKeySystemAccess>; vibrate(pattern: number | number[]): boolean; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; }
As a result, I ran into compilation errors because the compiler could not find the associated ServiceWorker types. Given that I'm new to JavaScript and TypeScript, I'm trying to determine the best way to continue. I understand the options:
- Save the js code and just ignore compilation errors (not ideal).
- Save the js code and somehow suppress type errors during compilation.
- Locate an existing TypeScript definition library that has
ServiceWorker defined and including that at compile time. - Write my own TypeScript definition file for the navigator or extend the existing
lib.d.ts
Sage's best advice is greatly appreciated.
Update
An attempt to apply to anyone to remove compilation errors, i.e.
var nav = <any> navigator; if ('serviceWorker' in nav) { nav.serviceWorker.register('sw.js') .then(function(reg) { console.log('yey!', <any> reg); }).catch(function(err) { console.log('boo!', <any> err); });
but now before new errors, i.e.
error TS7006: Parameter 'reg' implicitly has an 'any' type. error TS7006: Parameter 'error' implicitly has an 'any' type.
It is also tempting to write definitions for ServiceWorker using these details . However, I have never done this before, so it will take some practice!