-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(react-utilities): add custom RefAttributes
interface which is used within ForwardRefComponent to mitigate React.RefAttributes
expansion causing breaking changes in v8/v9 mixed context - shipped as patch release in @types/[email protected]
#34572
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
Hotell
merged 6 commits into
microsoft:master
from
Hotell:react-18/introduce-custom-ref-html-and-migrate
Jun 6, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
00a2557
feat: add custom RefAttributes interface which is used within Forward…
Hotell ff344ef
change file
Hotell 9297bfb
fix(eslint-plugin): add valid tsdoc tags to propagate when using reac…
Hotell 813b1fb
docs: udpate jsdoc
Hotell 8e00579
refactor(react-utilities): move RefAttributes to compose domain where…
Hotell 33d0ef5
fixup! refactor(react-utilities): move RefAttributes to compose domai…
Hotell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-utilities-66a3d122-bdde-45be-a7b3-d6ae2b28a1d6.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "feat: add custom RefAttributes interface which is used within ForwardRefComponent to mitigate breaking changes shipped as patch release in @types/[email protected]", | ||
"packageName": "@fluentui/react-utilities", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import * as React from 'react'; | ||
import { SLOT_CLASS_NAME_PROP_SYMBOL, SLOT_ELEMENT_TYPE_SYMBOL, SLOT_RENDER_FUNCTION_SYMBOL } from './constants'; | ||
import { DistributiveOmit, ReplaceNullWithUndefined } from '../utils/types'; | ||
import { DistributiveOmit, RefAttributes, ReplaceNullWithUndefined } from '../utils/types'; | ||
|
||
export type SlotRenderFunction<Props> = ( | ||
Component: React.ElementType<Props>, | ||
|
@@ -216,9 +216,15 @@ export type InferredElementRefType<Props> = ObscureEventName extends keyof Props | |
|
||
/** | ||
* Return type for `React.forwardRef`, including inference of the proper typing for the ref. | ||
* | ||
* @remarks | ||
* {@link React.RefAttributes} is {@link https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69756 | leaking string references} into `forwardRef` components | ||
* after introducing {@link https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68720 | RefAttributes Type Extension}, which shipped in `@types/[email protected]` | ||
* - `forwardRef` component do not support string refs. | ||
* - uses custom `RefAttributes` which is compatible with all React versions enforcing no `string` allowance. | ||
*/ | ||
export type ForwardRefComponent<Props> = React.ForwardRefExoticComponent< | ||
Props & React.RefAttributes<InferredElementRefType<Props>> | ||
Props & RefAttributes<InferredElementRefType<Props>> | ||
>; | ||
// A definition like this would also work, but typescript is more likely to unnecessarily expand | ||
// the props type with this version (and it's likely much more expensive to evaluate) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as React from 'react'; | ||
/** | ||
* Helper type that works similar to Omit, | ||
* but when modifying an union type it will distribute the omission to all the union members. | ||
|
@@ -31,3 +32,24 @@ export type UnionToIntersection<U> = (U extends unknown ? (x: U) => U : never) e | |
* If type T includes `null`, remove it and add `undefined` instead. | ||
*/ | ||
export type ReplaceNullWithUndefined<T> = T extends null ? Exclude<T, null> | undefined : T; | ||
|
||
/** | ||
* This type should be used in place of `React.RefAttributes<T>` in all components that specify `ref` prop. | ||
* | ||
* If user is using React 18 types `>=18.2.61`, they will run into type issues of incompatible refs, using this type mitigates this issues across react type versions. | ||
* | ||
* @remarks | ||
* | ||
* React 18 types introduced Type Expansion Change to the `RefAttributes` interface as patch release. | ||
* These changes were released in `@types/[email protected]` (replacing ref with `LegacyRef`, which leaks `string` into the union type, causing breaking changes between v8/v9 libraries): | ||
* - {@link https://github.com/DefinitelyTyped/DefinitelyTyped/pull/68720 | PR } | ||
* - {@link https://app.unpkg.com/@types/[email protected]/files/index.d.ts | shipped definitions } | ||
* | ||
* | ||
* In React 19 types this was "reverted" back to the original `Ref<T>` type. | ||
* In order to maintain compatibility with React 17,18,19, we are forced to use our own version of `RefAttributes`. | ||
* | ||
*/ | ||
export interface RefAttributes<T> extends React.Attributes { | ||
ref?: React.Ref<T> | undefined; | ||
Hotell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.