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

Bug: react-hooks/exhaustive-deps false positive when deps is defined with typescript const typing #20162

Closed
tranvansang opened this issue Nov 5, 2020 · 3 comments · Fixed by #28189
Labels
Component: ESLint Rules Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug

Comments

@tranvansang
Copy link

tranvansang commented Nov 5, 2020

React version: 17.0.1

Steps To Reproduce

  1. Setup eslint with @typescript-eslint/parser as parser
  2. Add as const to the deps array
function MyComp() {
	const [state, setState] = useState()
	useEffect(() => {
		console.log(state)
	}, [state] as const)
	return 'Hello, world'
}

Link to code example: https://github.com/tranvansang/exhaustive-deps-bug-1

The current behavior

The following errors were reported

  5:5  warning  React Hook useEffect was passed a dependency list that is not an array literal. This means we can't statically verify whether you've passed the correct dependencies  react-hooks/exhaustive-deps
  5:5  warning  React Hook useEffect has a missing dependency: 'state'. Either include it or remove the dependency array  

The expected behavior

The rule should interpret the deps argument correctly.

_

Why do I need to add as const to the deps array?

I build my own custom effect hook (with the additionalHooks option in .eslintrc), in which a tuple typing gives more type hint to the code.

@tranvansang tranvansang added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Nov 5, 2020
@forivall
Copy link

I'm having this issue too.

@eps1lon
Copy link
Collaborator

eps1lon commented Aug 17, 2021

Thanks for the report.

Would you be willing to add a test to

. Then one could pick up the work and fix the implementation.

@Emotico
Copy link

Emotico commented Aug 24, 2021

Hello, we are a group of researchers developing machine learning techniques to locate issues suitable for newcomers, and our model consider this issue as likely a "good first issue". May we recommend you to label it as "good first issue" so newcomers know where to choose?

Thank you!
@eps1lon

@eps1lon eps1lon closed this as completed in a1433ca Feb 1, 2024
github-actions bot pushed a commit that referenced this issue Feb 1, 2024
…ray (#28189)

## Summary

This PR closes #25844
The original issue talks about `as const`, but seems like it fails for
any `as X` expressions since it adds another nesting level to the AST.

EDIT: Also closes #20162

## How did you test this change?

Added unit tests

DiffTrain build for [a1433ca](a1433ca)
gaearon pushed a commit that referenced this issue Feb 3, 2024
…ray (#28189)

## Summary

This PR closes #25844
The original issue talks about `as const`, but seems like it fails for
any `as X` expressions since it adds another nesting level to the AST.

EDIT: Also closes #20162

## How did you test this change?

Added unit tests
EdisonVan pushed a commit to EdisonVan/react that referenced this issue Apr 15, 2024
…ray (facebook#28189)

## Summary

This PR closes facebook#25844
The original issue talks about `as const`, but seems like it fails for
any `as X` expressions since it adds another nesting level to the AST.

EDIT: Also closes facebook#20162

## How did you test this change?

Added unit tests
bigfootjon pushed a commit that referenced this issue Apr 18, 2024
…ray (#28189)

## Summary

This PR closes #25844
The original issue talks about `as const`, but seems like it fails for
any `as X` expressions since it adds another nesting level to the AST.

EDIT: Also closes #20162

## How did you test this change?

Added unit tests

DiffTrain build for commit a1433ca.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: ESLint Rules Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants