-
Notifications
You must be signed in to change notification settings - Fork 30.5k
[react]: Infer state type for useReducer #64412
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
Conversation
|
@mfp22 Thank you for submitting this PR! I see this is your first time submitting to DefinitelyTyped 👋 — I'm the local bot who will help you through the process of getting things through. This is a live comment which I will keep updated. 1 package in this PRCode ReviewsBecause this is a widely-used package, a DT maintainer will need to review it before it can be merged. You can test the changes of this PR in the Playground. Status
Once every item on this list is checked, I'll ask you for permission to merge and publish the changes. InactiveThis PR has been inactive for 31 days — it is considered abandoned, and therefore closed! Diagnostic Information: What the bot saw about this PR{
"type": "info",
"now": "-",
"pr_number": 64412,
"author": "mfp22",
"headCommitOid": "abf1a80ae12b0dba53c3216926288d673c685313",
"mergeBaseOid": "be21e88d0d7bd8d3714ae3af1baf9f9f925e456e",
"lastPushDate": "2023-04-23T03:09:48.000Z",
"lastActivityDate": "2023-04-23T03:47:18.000Z",
"hasMergeConflict": false,
"isFirstContribution": true,
"tooManyFiles": false,
"hugeChange": false,
"popularityLevel": "Critical",
"pkgInfo": [
{
"name": "react",
"kind": "edit",
"files": [
{
"path": "types/react/index.d.ts",
"kind": "definition"
},
{
"path": "types/react/test/hooks.tsx",
"kind": "test"
}
],
"owners": [
"johnnyreilly",
"bbenezech",
"pzavolinsky",
"ericanderson",
"DovydasNavickas",
"theruther4d",
"guilhermehubner",
"ferdaber",
"jrakotoharisoa",
"pascaloliv",
"hotell",
"franklixuefei",
"Jessidhia",
"saranshkataria",
"lukyth",
"eps1lon",
"zieka",
"dancerphil",
"dimitropoulos",
"disjukr",
"vhfmag",
"hellatan",
"priyanshurav",
"Semigradsky"
],
"addedOwners": [],
"deletedOwners": [],
"popularityLevel": "Critical"
}
],
"reviews": [
{
"type": "stale",
"reviewer": "eps1lon",
"date": "2023-02-22T21:23:18.000Z",
"abbrOid": "5eac079"
}
],
"mainBotCommentID": 1435649006,
"ciResult": "fail",
"ciUrl": "https://github.com/DefinitelyTyped/DefinitelyTyped/commit/abf1a80ae12b0dba53c3216926288d673c685313/checks?check_suite_id=12418770497"
} |
|
🔔 @johnnyreilly @bbenezech @pzavolinsky @ericanderson @DovydasNavickas @theruther4d @guilhermehubner @ferdaber @jrakotoharisoa @pascaloliv @Hotell @franklixuefei @Jessidhia @saranshkataria @lukyth @eps1lon @zieka @dancerphil @dimitropoulos @disjukr @vhfmag @hellatan @priyanshurav @Semigradsky — please review this PR in the next few days. Be sure to explicitly select |
types/react/index.d.ts
Outdated
| // NOTE 2: The past concern around autocompletion with the returned state object | ||
| // seems to no longer be an issue. See https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/63607 |
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.
We can remove NOTE and NOTE 2 now.
I can't even get autocomplete to work with the previous typings (down to TS 3.3): Playground with previous types
Proposed typings don't have autocomplete either: Playground with proposed types
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.
Strange. It works in the StackBlitz I made. If you go to line 40 and erase the count property and start typing again it autocompletes it. https://stackblitz.com/edit/react-ts-gy5gv6?file=App.tsx
Anyway, pipeline failed so I'm exploring that. And I'll erase those notes
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.
My overload is being used but the other specific overloads are causing my overload to not be able to infer the type. I will explore this more.
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.
@eps1lon I came up with something that might work. 2 overloads instead of 6. I just updated this PR.
Tests pass locally except for the test I added in this PR. I don't know why, because in VSCode running 5.0.0-dev20221218 It's correctly inferring how I want so the test should pass, and in the TS environment like what you shared, it's inferring correctly with v4.9.5. If it's failing for me locally, I bet it will fail in the pipeline. If it does fail, should I just remove my added test? It will pass existing tests and apparently sometimes be better than what's there now.
In VSCode it's autocompleting, and I just confirmed it is in the TS environment too.
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.
Less overloads is always a win. Especially if we can have 3 or less overloads since that has poor debuggability: microsoft/TypeScript#52745
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.
Yes it failed in the pipeline for some reason. I'll try to figure it out a bit longer, but I could just remove my test and make this a "simplify useReducer overloads" PR (maybe it fixes autocomplete too) and in every environment except whatever this specific one is, it will infer the reducer state like I wanted.

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.
I got it passing everything locally! $ExpectType is very picky and not helpful at telling you how you're using it wrong. But now I know.
|
@mfp22 The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! Note: builds which are failing do not end up on the list of PRs for the DT maintainers to review. |
|
@mfp22 The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! Note: builds which are failing do not end up on the list of PRs for the DT maintainers to review. |
|
Inspecting the JavaScript source for this package found some properties that are not in the .d.ts files. react (unpkg)was missing the following properties:
|
|
@eps1lon Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
eps1lon
left a comment
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.
Super excited to see this land while also reducing the number of overloads.
I need to double check, but I think this might cause some breakages if people specify the type parameters since some signatures are now gone. But I have to double check. Want to test this on some larger repos anyway before landing.
Leaving the requested changes mostly, so this doesn't get merged before we do some integration testing.
|
@mfp22 One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits. Thank you! |
|
@eps1lon Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
eps1lon
left a comment
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.
I love this change and it integrates pretty well (basically just removing explicit type parameters which is nice). However, the changes to the type parameter interface will require work.
That's why I think we should schedule this for the next React major versions so that we don't break any code relying on the current type parameters. I added it to #64451 so that we don't forget.
One thing I noticed when integrating is the following issue:
const reducer = (state: boolean, next?: unknown) => {
if (typeof next === 'boolean') {
return next
}
return !state
}
const useToggle = (initialState: boolean) => useReducer(reducer, initialState)
const [isTooltipVisible, toggleTooltipVisibility] = useToggle(false)
toggleTooltipVisibility(false)
// ^^^^^ Expected 0 arguments, but got 1Is this something we can fix?
|
@mfp22 One or more reviewers has requested changes. Please address their comments. I'll be back once they sign off or you've pushed new commits. Thank you! |
Oh that could be years away... too bad. Sure we can't just YOLO this into React 18? I'll volunteer to fix everyone's problems :)
The only way I've found to solve this case is to spread the action arguments onto the dispatch function directly. That preserves optional parameters, but would allow potentially multiple arguments to be passed into So, I changed I added tests to reject reducers with extra parameters, and a test to make sure optional action parameters are preserved. If someday they change |
|
@eps1lon Thank you for reviewing this PR! The author has pushed new commits since your last review. Could you take another look and submit a fresh review? |
|
Re-ping @johnnyreilly, @bbenezech, @pzavolinsky, @ericanderson, @DovydasNavickas, @theruther4d, @guilhermehubner, @ferdaber, @jrakotoharisoa, @pascaloliv, @Hotell, @franklixuefei, @Jessidhia, @saranshkataria, @lukyth, @eps1lon, @zieka, @dancerphil, @dimitropoulos, @disjukr, @vhfmag, @hellatan, @priyanshurav, @Semigradsky: This PR has been out for over a week, yet I haven't seen any reviews. Could someone please give it some attention? Thanks! |
|
It has been more than two weeks and this PR still has no reviews. I'll bump it to the DT maintainer queue. Thank you for your patience, @mfp22. (Ping @johnnyreilly, @bbenezech, @pzavolinsky, @ericanderson, @DovydasNavickas, @theruther4d, @guilhermehubner, @ferdaber, @jrakotoharisoa, @pascaloliv, @Hotell, @franklixuefei, @Jessidhia, @saranshkataria, @lukyth, @eps1lon, @zieka, @dancerphil, @dimitropoulos, @disjukr, @vhfmag, @hellatan, @priyanshurav, @Semigradsky.) |
|
When is react 19 going to release? I'm not happy about having this PR open for a long time, but if it's reasonably soon, then it's not too bad. |
Should we convert to a draft in the meantime instead? Or we can also close in the meantime. Though it's not clear to me what's so special of having one more open. |
|
Draft should be good - that aughta remove it from the maintainer queue. |
|
@mfp22 Unfortunately, this pull request currently has a merge conflict 😥. Please update your PR branch to be up-to-date with respect to master. Have a nice day! |
|
@mfp22 I haven't seen any activity on this PR in more than three weeks, and it still has problems that prevent it from being merged. The PR will be closed on Apr 26th (in a week) if the issues aren't addressed. |
|
@mfp22 The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! Note: builds which are failing do not end up on the list of PRs for the DT maintainers to review. |
|
@mfp22 The CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! Note: builds which are failing do not end up on the list of PRs for the DT maintainers to review. |
|
@eps1lon I don't understand how this build could have failed with just the changes in this PR. It looks like it doesn't have anything to do with I also noticed a |
|
@mfp22 I haven't seen any activity on this PR in more than three weeks, and it still has problems that prevent it from being merged. The PR will be closed on May 23rd (in a week) if the issues aren't addressed. |
|
@mfp22 To keep things tidy, we have to close PRs that aren't mergeable and don't have activity in the last month. No worries, though — please open a new PR if you'd like to continue with this change. Thank you! |
|
@eps1lon Does this mean this PR can be resurrected soon? https://www.reddit.com/r/reactjs/comments/1ajur2f/is_there_any_news_on_react_19/ |
|
It will be part of the React 19 release. I already have a branch that includes the commits from this PR. |
npm test <package to test>.Select one of these and delete the others:
If changing an existing definition: