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

feat(TS): migrate to TypeScript #76

Merged
merged 3 commits into from
Nov 29, 2020
Merged

feat(TS): migrate to TypeScript #76

merged 3 commits into from
Nov 29, 2020

Conversation

kentcdodds
Copy link
Collaborator

@kentcdodds kentcdodds commented Nov 27, 2020

What: Migrate to TypeScript

Why: To have much better type definitions and improved confidence

How: upgrade all deps and leverage the new TS features from kcd-scripts

Checklist:

  • Documentation N/A
  • Tests
  • Ready to be merged

src/index.tsx Outdated
| ErrorBoundaryPropsWithRender

type ErrorBoundaryState = {error: Error | null}
const initialState = {error: null}
Copy link

Choose a reason for hiding this comment

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

👋 you need to specify:

const initialState: ErrorBoundaryState = { error: null }

Otherwise the typeof state = initialState will be { error: null }. This is what the rule @typescript-eslint/no-unnecessary-condition is complaining about, error !== null is unnecessary because it's typed as error: null so from TS perspective error !== null will never be true.

This is because of the generic {error: Error | null}. Since { error: null } extends ErrorBoundaryState then it's inferred as { error: null } when you do state = initialState.

Alternatively, you could set in line 70:

state: ErrorBoundaryState = initialState

And then you'll be able to remove the comments regarding @typescript-eslint/no-unnecessary-condition

I hope this makes sense 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes perfect sense. And it works. Thank you!

needs: main
runs-on: ubuntu-latest
if:
${{ github.repository == 'kentcdodds/react-error-boundary' &&
Copy link

Choose a reason for hiding this comment

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

Shouldn't this be bvaughn/react-error-boundary? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup! Thank you.

src/index.tsx Outdated
const initialState = {error: null}
class ErrorBoundary extends React.Component<
// TODO: why is there not a PropsWithRefAndChildren? 🤔
React.PropsWithRef<ErrorBoundaryProps>,
Copy link

Choose a reason for hiding this comment

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

Maybe?:

React.PropsWithRef<React.PropsWithChildren<ErrorBoundaryProps>>,

😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense 😆 Thanks!

@@ -0,0 +1,128 @@
# Contributor Covenant Code of Conduct

Choose a reason for hiding this comment

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

Did you guys discuss the move from https://github.com/bvaughn/react-error-boundary/blob/master/other/CODE_OF_CONDUCT.md (http://contributor-covenant.org/version/1/4) to this version (https://www.contributor-covenant.org/version/2/0/)? The commit message makes it sound like the change was accidental...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've been slowly upgrading all of my OSS projects to the latest version.

Copy link

@rbusquet rbusquet left a comment

Choose a reason for hiding this comment

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

Left a couple of comments

@@ -39,20 +39,22 @@ test('handleError forwards along async errors', async () => {

await screen.findByRole('alert')

const [[actualError], [componentStack]] = console.error.mock.calls
const firstLineOfError = firstLine(actualError)
const consoleError = console.error as jest.Mock<void, unknown[]>

Choose a reason for hiding this comment

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

👍

src/index.tsx Outdated
const props = ({
error,
resetErrorBoundary: this.resetErrorBoundary,
} as unknown) as FallbackProps

Choose a reason for hiding this comment

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

I don't get why you need the type cast if the shape looks correct...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can't remember why, but it complained about it at one point. I don't like type casting at all, but I wanted to get it committed 😅

Removing it now, and it's fine with it as-is so 🎉

src/index.tsx Outdated
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (error !== null && changedArray(prevProps.resetKeys, resetKeys)) {
this.props.onResetKeysChange?.(
prevProps.resetKeys as Array<unknown>,

Choose a reason for hiding this comment

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

I wonder if making type generic in the type of reset keys (likeArray<T>) would help with type casting.

I'm of the opinion those should be used sparingly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, I figured out another way to fix this that didn't require type casting 👍

@kentcdodds
Copy link
Collaborator Author

Not sure why the snapshots are failing 🤔 Will look into it later 😬

@codecov-io
Copy link

Codecov Report

Merging #76 (d56f09a) into master (f0c2266) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #76   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           40        48    +8     
  Branches        11        11           
=========================================
+ Hits            40        48    +8     
Impacted Files Coverage Δ
src/index.tsx 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 f0c2266...d56f09a. Read the comment docs.

@kentcdodds
Copy link
Collaborator Author

Looks like the snapshot issue was related to Node 10. I don't care all that much to make sure the build runs on Node 10, so I just removed it.

@kentcdodds kentcdodds merged commit 947035a into master Nov 29, 2020
@kentcdodds kentcdodds deleted the pr/ts branch November 29, 2020 03:49
@github-actions
Copy link

🎉 This PR is included in version 3.1.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.

None yet

6 participants