core: fix NodeDetails type mismatch#11752
Merged
devtools-bot merged 2 commits intomasterfrom Dec 2, 2020
Merged
Conversation
brendankenny
commented
Dec 2, 2020
| id: script.id || null, | ||
| async: script.async, | ||
| defer: script.defer, | ||
| source: /** @type {'head'|'body'} */ (script.closest('head') ? 'head' : 'body'), |
Contributor
Author
There was a problem hiding this comment.
this cast does nothing because the untyped ...getNodeDetails() below is spreading ...any which just spreads into the whole object being any :)
brendankenny
commented
Dec 2, 2020
| devtoolsNodePath: string, | ||
| selector: string, | ||
| boundingRect: Rect | null, | ||
| boundingRect?: Rect, |
Contributor
Author
There was a problem hiding this comment.
This is the functional change to the PR. In practice it shouldn't be observable because I believe null could only ever come from link-elements and script-elements and we wouldn't have been trying to put their boundingRects in the LHR in the first place, but you never know where some code is checking against undefined instead of falsiness or something.
connorjclark
approved these changes
Dec 2, 2020
Contributor
Author
have I got a follow up for you :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I was reviewing some code recently, noticed we're being very aggressive in some type casts of audit details to
/** @type {LH.Audit.Details.NodeValue} */, removed some of them, and found a type error due to theNodeDetailschanges in #11405 that wasn't flagged due to the casts hiding the difference between types (in this case, anullproperty in the artifact being used for an optional (but not nullable) property in the audit details).We were using that cast because if strings are nested in an object, they're typically widened by tsc from the literal value to just
string, which means they're no longer validAuditDetails(which need a specific literal). But casts are dangerous (this is a pretty mild example of how they can go wrong), so we should avoid if we can.There are two main options instead of casting:
cast just the string
type: /** @type {'node'} */ ('node')This is annoying, but it's about as painful as in the typescript world (there you could add
as constto all of these strings)type the container these are going into.
This has improved since we originally added these types, where even if the string literal is several layers deep in an object literal, if it's going into e.g. this example array that's typed with that audit details, the string won't be widened
(2) seems like they better solution, and with details items like
/** @type {Array<{node: LH.Audit.Details.NodeValue}>} */it's really not so bad. But several of these have more properties (href,target,rel...) and so the type starts getting excessive compared to just letting it be inferred.As a result, I went with option 1, but completely happy to go the other way instead.
Either way, hopefully this will set the template for how to items in future audits and we can stamp out some future casting :)