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

core(tsc): NetworkRequest.RESOURCE_TYPES type fix #5851

Merged
merged 2 commits into from
Aug 17, 2018
Merged

Conversation

brendankenny
Copy link
Member

After adding SelfMap in #5849, went through all our uses of Record<> to see if we actually meant that the keys need to be mapped to themselves. This is the only case I could find.

Unfortunately that meant that some other types needed to be widened to accommodate this, but good to have the type of RESOURCE_TYPES.Fetch be 'Fetch' and not 'XHR'|'Fetch'|'EventSource'|'Script'|... :)

@@ -20,6 +20,7 @@ const CHROME_EXTENSION_PROTOCOL = 'chrome-extension:';
const compressionHeaders = ['content-encoding', 'x-original-content-encoding'];
const compressionTypes = ['gzip', 'br', 'deflate'];
const binaryMimeTypes = ['image', 'audio', 'video'];
/** @type {Array<string>} */
Copy link
Collaborator

Choose a reason for hiding this comment

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

so Array<LH.Crdp.Page.ResourceType> won't work anymore? Maybe I'm confused but I'm not sure why it's better now :)

What are the good cases we expect to catch by having Network.TYPES.Fetch be just 'Fetch' instead of LH.Crdp.Page.ResourceType?

Copy link
Member Author

Choose a reason for hiding this comment

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

so Array<LH.Crdp.Page.ResourceType> won't work anymore?

uh, no, that will work and is way better :) fixing now.

The issue was that these are defined with const strings, so tsc wasn't widening them at all. e.g.

const textResourceTypes = [
  NetworkRequest.TYPES.Document,
  NetworkRequest.TYPES.Script,
  NetworkRequest.TYPES.Stylesheet,
];

was becoming only type Array<'Document'|'Script'|'Stylesheet'>, so textResourceTypes.includes() wasn't allowing you to pass it the general LH.Crdp.Page.ResourceType type (see microsoft/TypeScript#26255 for more on this annoyance :)

But Array<LH.Crdp.Page.ResourceType> is a way better fix than Array<string> for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

What are the good cases we expect to catch by having Network.TYPES.Fetch be just 'Fetch' instead of LH.Crdp.Page.ResourceType?

basically the inverse of the situation with the arrays. e.g if you had doSomething(resourceType: 'Fetch'|'XHR'), passing it NetworkRequest.TYPES.Fetch will be an error (since it's the full union). Not an issue in our current code, but it could be in the future.

(but it sounds like you're ok with this without the regression to Array<string> in the original form of the PR?)

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

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

cool cool 👍

@brendankenny brendankenny merged commit dee7411 into master Aug 17, 2018
@brendankenny brendankenny deleted the resource_types branch August 17, 2018 17:47
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.

2 participants