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

Strict TypeScript cleanup #2244

Merged
merged 1 commit into from
Oct 4, 2019
Merged

Strict TypeScript cleanup #2244

merged 1 commit into from
Oct 4, 2019

Conversation

jeffposnick
Copy link
Contributor

R: @philipwalton

Fixes #2242

This cleans up a number of problems that were found by switching to using strict: true in the TypeScript compiler options.

There are some... questionable?... casts going on, so I'll address them via comments on the specific lines to see whether there are better approaches.

const objStore = txn.objectStore(storeName)
const objStore = txn.objectStore(storeName);
// TODO(philipwalton): Fix this underlying TS2684 error.
// @ts-ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the biggest issue, and I just punted on it as I don't have any idea how to resolve it. @philipwalton, if you can look into it as part of either another PR or push an update to this PR, that would be great.

@@ -19,7 +19,8 @@ import './_version.js';
* @alias workbox.precaching.cleanupOutdatedCaches
*/
export const cleanupOutdatedCaches = () => {
addEventListener('activate', (event: ExtendableEvent) => {
// See https://github.com/Microsoft/TypeScript/issues/28357#issuecomment-436484705
Copy link
Contributor Author

@jeffposnick jeffposnick Oct 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of similar changes to this one, due to TypeScript being unhappy about the listener function taking a parameter of type ExtendableEvent.

Declaring these functions as being of type EventListener seems fairly "safe", but perhaps the more official route would be to override the addEventListener definition so that it supports a listener function that takes in an ExtendableEvent parameter, instead of just an Event parameter.

@@ -128,11 +130,10 @@ class Router {

// If a MessageChannel was used, reply to the message on success.
if (event.ports && event.ports[0]) {
await requestPromises;
event.ports[0].postMessage(true);
requestPromises.then(() => event.ports[0].postMessage(true));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declaring the listener function as being an EventListener wouldn't work if it was async, since EventListeners aren't supposed to return Promise<void>.

So I just took the one await and converted it into a then(), and dropped the async from the listener function declaration.

@@ -27,7 +27,7 @@ export class WorkboxEventTarget {
*/
addEventListener<K extends keyof WorkboxEventMap>(type: K, listener: (event: WorkboxEventMap[K]) => any) {
const foo = this._getEventListenersByType(type)
foo.add(listener);
foo.add(listener as ListenerCallback);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the other place where I declare listener as being a ListenerCallback might be a little sketchy, so your thoughts are appreciated.

The underlying error is because some varieties of listener might have a data property (the one that wraps a message event), and TypeScript isn't happy about that.

There might be a better remediation here, like changing the _getEventListenersByType() definition so that it returns a Set with different signature.

@workbox-pr-bot
Copy link
Collaborator

PR-Bot Size Plugin

Changed File Sizes

File Before After Change GZipped
packages/workbox-broadcast-update/build/workbox-broadcast-update.prod.js 1.27 KB 1.30 KB +2% 720 B
packages/workbox-routing/build/workbox-routing.prod.js 3.09 KB 3.08 KB -0% 1.35 KB
packages/workbox-window/build/workbox-window.dev.umd.js 41.51 KB 41.47 KB -0% 9.19 KB

New Files

No new files have been added.

All File Sizes

View Table
File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.87 KB 3.87 KB 0% 1.60 KB
packages/workbox-broadcast-update/build/workbox-broadcast-update.prod.js 1.27 KB 1.30 KB +2% 720 B
packages/workbox-build/build/_types.js 41 B 41 B 0% 61 B
packages/workbox-build/build/generate-sw.js 2.75 KB 2.75 KB 0% 1.23 KB
packages/workbox-build/build/get-manifest.js 1.60 KB 1.60 KB 0% 779 B
packages/workbox-build/build/index.js 613 B 613 B 0% 344 B
packages/workbox-build/build/inject-manifest.js 4.93 KB 4.93 KB 0% 1.90 KB
packages/workbox-cacheable-response/build/workbox-cacheable-response.prod.js 610 B 610 B 0% 356 B
packages/workbox-cli/build/app.js 4.16 KB 4.16 KB 0% 1.64 KB
packages/workbox-cli/build/bin.js 940 B 940 B 0% 502 B
packages/workbox-core/build/workbox-core.prod.js 6.80 KB 6.80 KB 0% 2.82 KB
packages/workbox-expiration/build/workbox-expiration.prod.js 2.95 KB 2.95 KB 0% 1.27 KB
packages/workbox-google-analytics/build/workbox-offline-ga.prod.js 1.97 KB 1.97 KB 0% 915 B
packages/workbox-navigation-preload/build/workbox-navigation-preload.prod.js 659 B 659 B 0% 323 B
packages/workbox-precaching/build/workbox-precaching.prod.js 4.95 KB 4.95 KB 0% 1.92 KB
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.64 KB 1.64 KB 0% 808 B
packages/workbox-routing/build/workbox-routing.prod.js 3.09 KB 3.08 KB -0% 1.35 KB
packages/workbox-strategies/build/workbox-strategies.prod.js 4.10 KB 4.10 KB 0% 1.13 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.44 KB 1.44 KB 0% 698 B
packages/workbox-sw/build/workbox-sw.js 1.34 KB 1.34 KB 0% 746 B
packages/workbox-webpack-plugin/build/generate-sw.js 4.22 KB 4.22 KB 0% 1.61 KB
packages/workbox-webpack-plugin/build/index.js 349 B 349 B 0% 255 B
packages/workbox-webpack-plugin/build/inject-manifest.js 6.94 KB 6.94 KB 0% 2.33 KB
packages/workbox-window/build/workbox-window.dev.umd.js 41.51 KB 41.47 KB -0% 9.19 KB
packages/workbox-window/build/workbox-window.prod.umd.js 4.52 KB 4.52 KB 0% 1.85 KB

Workbox Aggregate Size Plugin

3.51KB gzip'ed (23% of limit)
7.93KB uncompressed

@jeffposnick jeffposnick merged commit a555d06 into master Oct 4, 2019
@jeffposnick jeffposnick deleted the ts-strict branch October 4, 2019 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use strict: true tsc option
3 participants