-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
chore(gatsby): convert sanitize-node to typescript #36327
chore(gatsby): convert sanitize-node to typescript #36327
Conversation
@@ -20,15 +68,15 @@ const sanitizeNode = (data, isNode = true, path = new Set()) => { | |||
returnData[key] = o | |||
return | |||
} | |||
returnData[key] = sanitizeNode(o, false, path) | |||
returnData[key] = sanitizeNode(o as data, false, path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change this loop to a Object.entries(data).forEach
? That should allow TS to pick up the right types and we shouldn't need to cast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.entries
hardcodes the first value o
as string, making tests fail (Symbol/string issue, .each
avoids this issue).
|
||
if (returnData[key] !== o) { | ||
anyFieldChanged = true | ||
} | ||
}) | ||
|
||
if (anyFieldChanged) { | ||
data = omitUndefined(returnData) | ||
data = omitUndefined(returnData as data) as data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can type returnData
during initialization, we should be able to get away with no casting here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returnData
is initalised as {}
or []
.
{}
is not a validIGatsbyNode
error: Type '{}' is missing the following properties from type 'IGatsbyNode': id, parent, children, internal, fields[]
is not a validGatsbyIterable<IGatsbyNode>
error:
Type 'never[]' is missing the following properties from type 'GatsbyIterable': source, deduplicate, mergeSorted, intersectSorted, deduplicateSorted
I could cast when we define returnData
to avoid the double casting here, but we would still need one as omitUndefined
returns a Partial<data>
(from lodash, pickBy returns a Partial
).
I think it's down to personal preference here, I feel the double cast here is more significant than having it when we define returnData
but I'm open to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable enough. Thank you for the detailed response. 😄
…ert-sanitize-node-to-typescript
…il/gatsby into convert-sanitize-node-to-typescript
Code looks good! Got builds kicked off and I'll check back later. 😄 |
Thanks again @Kornil . We appreciate your patience and all of your contributions to Gatsby 💜 |
Convert sanitize-node to TS.
Related Issues
#21995