-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Update useRef types to reflect normal usage #64855
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
Update useRef types to reflect normal usage #64855
Conversation
|
@ericanderson Thank you for submitting this PR! 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": 64855,
"author": "ericanderson",
"headCommitOid": "7f33356aa39c21ddbb33f66a3de9f9079da1f1a9",
"mergeBaseOid": "1d9d491c90bd1c852dea2a9ff36a7cf0bf48cd7c",
"lastPushDate": "2023-03-22T18:18:39.000Z",
"lastActivityDate": "2023-03-28T13:05:53.000Z",
"hasMergeConflict": false,
"isFirstContribution": false,
"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": [],
"mainBotCommentID": 1480068687,
"ciResult": "fail",
"ciUrl": "https://github.com/DefinitelyTyped/DefinitelyTyped/commit/7f33356aa39c21ddbb33f66a3de9f9079da1f1a9/checks?check_suite_id=11740599047"
} |
|
🔔 @johnnyreilly @bbenezech @pzavolinsky @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 |
|
@ericanderson 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. |
|
I missed an edge case that isn't quite acceptable that I am still trying to work through. |
| readonly current: T | null; | ||
| } | ||
| /** | ||
| * @deprecated All refs are mutable. This type has always been wrong. Expect it to change to |
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.
This wording doesn't really add value. There was a rationale why we did this. It's like saying ReadonlyArray<SomeArray> "was always wrong". Just because something works in JavaScript, doesn't mean it's correct.
I don't think we should deprecate this. Doesn't it make more sense to drop MutableRefObject in the future instead and let RefObject be the mutable one?
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.
Originally, MutableRefs meant user-managed while readonly-refs meant react-managed. See https://github.com/DefinitelyTyped/DefinitelyTyped/pull/64772/files#r1148507326
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 reviewed the history prior to making the recommendation. However there isn't really a way to know if the intended use is user managed vs react-managed, which is unfortunate.
Changing RefObject to be representable with MutableRefObject allows for deprecation. The reverse isn't possible.
I'm saying its "wrong" because really you can change it as you see fit and applying magic typing to try to signify if something is readonly or not is just confusing. I'm saying its "wrong" because we created a weird mechanism in the types that are inconsistent with react itself and have been since day one.
Also, saying the ref is 'readonly' when you pass it to something that clearly modifies the value, also breaks most of the ways that readonly is used, which is that it lets you know that the thing you are giving it to doesn't mutate it. To your example of a ReadonlyArray, if an API had a doFoo() that returned {foo: ReadonlyArray<T>} then I would assume that the inner array would never change out from under me. But in the analogy, you're allowing it to change by react.
|
#64896 for an alternate proposal. Keeping |
|
@ericanderson 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 27th (in a week) if the issues aren't addressed. |
|
@ericanderson 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! |
Updates to reflect concern brought up by @gaearon in #64772 regarding this tweet.
Specifically:
RefObjectis now deprecated.refpreviously only allowed ref variants that looked like{ current: T | null } | { current: T }and they now accept variants that are like{ current: T | undefined }.