Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript confusion #2860

Closed
ivandotv opened this issue Jun 4, 2021 · 4 comments · Fixed by #2870
Closed

Typescript confusion #2860

ivandotv opened this issue Jun 4, 2021 · 4 comments · Fixed by #2870
Labels
Feature Request TypeScript Issues related to our TypeScript annotations. workbox-window

Comments

@ivandotv
Copy link

ivandotv commented Jun 4, 2021

Library Affected:
workbox-window
Browser & Platform:
"all browsers".*

Issue or Feature Request Description:
I'm trying to use workbox-window in Typescript however, the package doesn't export any event type declarations?

wb.addEventListener('waiting', (evt: WorkboxLifecycleWaitingEvent) => {})  // error

WorkboxLifecycleWaitingEvent is not exported from workbox-window

@jeffposnick
Copy link
Contributor

Hello!

While it's true that WorkboxLifecycleWaitingEvent is not explicitly exported, that's because you should never need to instantiate an instance from your own code. You're passed an instance of WorkboxLifecycleWaitingEvent via the waiting callback as the first parameter, and there's no need to explicitly state its type like you're doing in the code snippet—TypeScript knows that it's a WorkboxLifecycleWaitingEvent automatically:

Screen Shot 2021-06-10 at 6 56 26 PM

I think that that's the standard way for library authors to deal with this sort of thing, and is therefore not something that we have plans to change. If I'm misunderstanding a use case, or if this breaks something for you, please let me know and we can reevaluate.

@jeffposnick jeffposnick added TypeScript Issues related to our TypeScript annotations. workbox-window labels Jun 11, 2021
@ivandotv
Copy link
Author

ivandotv commented Jun 11, 2021

That is true, only if you create the function inline.

wb.addEventListener('waiting', handleWaiting)  // error
wb.addEventListener('waiting', myClassInstance.handleWaiting)  // error

The first case is very common.
Also , ESLint can give you an error there, depending on your configuration.

This is very limiting

@jeffposnick jeffposnick reopened this Jun 11, 2021
@jeffposnick
Copy link
Contributor

Fair enough! We can get those event definitions exported as part of an upcoming Workbox v6 release.

@jeffposnick
Copy link
Contributor

The fix is now deployed in a pre-release of Workbox v6.2.0: https://github.com/GoogleChrome/workbox/releases/tag/v6.2.0-alpha.0

ivandotv added a commit to ivandotv/live-radio that referenced this issue Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request TypeScript Issues related to our TypeScript annotations. workbox-window
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants