Skip to content

Commit

Permalink
[flow][libdef] Update typing for React.Context to align with React …
Browse files Browse the repository at this point in the history
…19 changes

Summary:
In React 19, created react context `MyContext` can be used in places that expects `MyContext.Provider`. This diff updates the libdef to support that.

This is implemented as an intersection type behind an opaque type.

Close #9245

Changelog: [lib] Type for `React.Context` has been updated to support [React 19's context as provider model](https://react.dev/blog/2024/12/05/react-19#context-as-a-provider). Given this change, you might need additional annotations on exports. e.g.

```
const Context: React.Context<Foo> = React.createContext(...);

export const MyProvider = Context.Provider;
//             ^ annotation now required
```

Reviewed By: alexmckenley

Differential Revision: D67993213

fbshipit-source-id: cb717f4449a39d8614143f84e11990704462b6f4
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 13, 2025
1 parent 10f17c1 commit 0d9ba1a
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 304 deletions.
6 changes: 5 additions & 1 deletion lib/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ declare type React$RefSetter<-T> =
* The type of a React Context. React Contexts are created by calling
* createContext() with a default value.
*/
declare type React$Context<T> = {
declare opaque type React$Context<T>: React$ComponentType<{
+value: T,
+children?: React$Node,
...
}> & {
Provider: React$ComponentType<{
+value: T,
+children?: React$Node,
Expand Down
4 changes: 2 additions & 2 deletions tests/badly_positioned_react/badly_positioned_react.exp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ References:
test.js:21:29
21| const Foo1 = create(create1<{||}>(Foo));
^^^^ [2]
<BUILTINS>/react.js:349:38
349| declare export type ComponentType<-P> = React$ComponentType<P>;
<BUILTINS>/react.js:353:38
353| declare export type ComponentType<-P> = React$ComponentType<P>;
^ [3]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Cannot cast `3` to `React.FragmentType` because number [1] is incompatible with
^ [1]

References:
<BUILTINS>/react.js:748:5
<BUILTINS>/react.js:752:5
v------------------------------------------------------
748| declare component Fragment<Renders: React$Node = void>(
749| children?: Renders,
750| ) renders Renders;
752| declare component Fragment<Renders: React$Node = void>(
753| children?: Renders,
754| ) renders Renders;
-----------------^ [2]


Expand Down
16 changes: 8 additions & 8 deletions tests/component_type/component_type.exp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ References:
component_to_component.js:26:37
26| x: component(ref: React.RefSetter<Component>, ...{...}),
^^^^^^^^^ [2]
<BUILTINS>/react.js:365:34
365| declare export type RefSetter<-T> = React$RefSetter<T>;
<BUILTINS>/react.js:369:34
369| declare export type RefSetter<-T> = React$RefSetter<T>;
^ [3]


Expand Down Expand Up @@ -426,8 +426,8 @@ Cannot create `C` element because in property `ref`: [incompatible-type]
^^^^^^

References:
<BUILTINS>/react.js:698:8
698| ): React$RefObject<T | null>;
<BUILTINS>/react.js:702:8
702| ): React$RefObject<T | null>;
^^^^^^^^^^^^^^^^^^^^^^^^^ [1]
<BUILTINS>/react.js:197:6
197| | ((T | null) => mixed)
Expand Down Expand Up @@ -575,8 +575,8 @@ References:
<BUILTINS>/core.js:2060:19
2060| declare class Set<T> extends $ReadOnlySet<T> {
^ [3]
<BUILTINS>/react.js:235:19
235| type RefSetter<-T> = React$RefSetter<T>;
<BUILTINS>/react.js:239:19
239| type RefSetter<-T> = React$RefSetter<T>;
^ [4]


Expand Down Expand Up @@ -991,8 +991,8 @@ References:
<BUILTINS>/core.js:2060:19
2060| declare class Set<T> extends $ReadOnlySet<T> {
^ [3]
<BUILTINS>/react.js:235:19
235| type RefSetter<-T> = React$RefSetter<T>;
<BUILTINS>/react.js:239:19
239| type RefSetter<-T> = React$RefSetter<T>;
^ [4]


Expand Down
4 changes: 2 additions & 2 deletions tests/new_generics/new_generics.exp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,8 +1031,8 @@ References:
react.js:13:28
13| a as React.ComponentType<{a: number}>; // nope, contravariance
^^^^^^^^^^^ [2]
<BUILTINS>/react.js:349:38
349| declare export type ComponentType<-P> = React$ComponentType<P>;
<BUILTINS>/react.js:353:38
353| declare export type ComponentType<-P> = React$ComponentType<P>;
^ [3]


Expand Down
Loading

0 comments on commit 0d9ba1a

Please sign in to comment.