Skip to content

chore: Convert screen.js to TypeScript #1002

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

Merged
merged 7 commits into from
Aug 25, 2021

Conversation

riotrah
Copy link
Contributor

@riotrah riotrah commented Aug 6, 2021

What:

Converting the following to TS:

  • src/screen.js -> src/screen.ts

Why:

#494 outlines motivations and status of project TS conversion, for which this is the start of my contribution.

How:

Renamed file. Found type errors and started to fix them whilst trying to keep runtime logic intact.

Checklist:

  • Documentation added to the
    docs site N/A
  • Tests
  • TypeScript definitions updated N/A
  • Ready to be merged
  1. Should I squash my commits?
  2. Do TS defs need to be updated? From what I understand kcd-scripts builds d.ts files from the npm run build script.
  3. I had some coding style/type strictness questions that I've described in the form of "Grok Flow", at this link:

https://dev.app.trygrok.com/pl/YQHa2AMeAQAeanwo/imO5GqKqQI-sE1jrFFUWjg

It's a mind-map style format for describing code-adjacent metadata and comments. At my company we use these for internal not-documentation for major TS migrations or overhauls and also for PR/MRs to describe changes/questions. We made this beta web viewer version recently after some of wanted to use Flows for our personal OSS contributions. Please let me know if you would prefer that I inline those questions into this issue.

@riotrah riotrah changed the title chore(types): Add @types for lz-string TS Conversion > src/screen.js Aug 6, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 6, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9dcdf7f:

Sandbox Source
react-testing-library-examples Configuration

@riotrah riotrah force-pushed the pr/ts/src/screen branch 2 times, most recently from 8322829 to ed56210 Compare August 6, 2021 19:16
@riotrah riotrah marked this pull request as ready for review August 6, 2021 19:19
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Looks good overall except the Object.keys helper.

src/screen.ts Outdated
Comment on lines 47 to 49
const initialValue: {
[key in keyof typeof queries | 'debug' | 'logTestingPlaygroundURL']?: Function
} = {debug, logTestingPlaygroundURL}
Copy link
Member

Choose a reason for hiding this comment

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

Does

Suggested change
const initialValue: {
[key in keyof typeof queries | 'debug' | 'logTestingPlaygroundURL']?: Function
} = {debug, logTestingPlaygroundURL}
const initialValue = {debug, logTestingPlaygroundURL} as const

work as well?

src/screen.ts Outdated
? getQueriesForElement(document.body, queries, initialValue)
: Object.keys(queries).reduce((helpers, key) => {
: typedKeysOf(queries).reduce<typeof initialValue>((helpers, key) => {
Copy link
Member

Choose a reason for hiding this comment

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

There's a reason why Object.keys returns string[] and not an array of literals: https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript. Let's stick with Object.keys and annotate the types properly (casting, @ts-expect-error etc).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think in this case perhaps a helpers[key as keyof typeof initialValue] might work best. A @ts-expect-error might work too, however, anyone familiar with the Object.keys caveat may be instantly familiar with the nature and reason of the inline cast.

Though I'm down for whatever! I'll push my proposed change and you can let me if it works.

In addition, doing the cast here makes the explicit typing of initialValue above and the explicit type param passed to reduce here unneccessary as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Double post question: Do y'all care about squashing commits? EG should I squash the aforementioned upcoming commit into the one last pushed?

Copy link
Member

Choose a reason for hiding this comment

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

Double post question: Do y'all care about squashing commits? EG should I squash the aforementioned upcoming commit into the one last pushed?

During review on github it's usually best to not rebase so that notifications only display the most recent changes. We squash on merge anyway.

Copy link
Member

Choose a reason for hiding this comment

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

I think in this case perhaps a helpers[key as keyof typeof initialValue] might work best.

Yeah. @ts-expect-error would also hide potential future errors. But let's document why we think the cast is safe and link the Object.keys caveat.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed

@eps1lon eps1lon changed the title TS Conversion > src/screen.js chore: Convert screen.js to TypeScript Aug 25, 2021
@codecov
Copy link

codecov bot commented Aug 25, 2021

Codecov Report

Merging #1002 (9dcdf7f) into main (992f8b5) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #1002   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines          916       916           
  Branches       285       284    -1     
=========================================
  Hits           916       916           
Flag Coverage Δ
node-12 100.00% <100.00%> (ø)
node-14 100.00% <100.00%> (ø)
node-16 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/screen.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 992f8b5...9dcdf7f. Read the comment docs.

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Thanks! I revert the change in snapshots since they are specific to local environment (we're unclear why).

Also matched the types of screen.d.ts (debug accepts a single HTMLElement and Document)

@eps1lon eps1lon merged commit 066a41f into testing-library:main Aug 25, 2021
@eps1lon
Copy link
Member

eps1lon commented Aug 25, 2021

@all-contributors Add @riotrah for code

@allcontributors
Copy link
Contributor

@eps1lon

I've put up a pull request to add @riotrah! 🎉

@github-actions
Copy link

🎉 This PR is included in version 8.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants