Skip to content

Commit dce2b52

Browse files
committed
jsx4: fix incorrect type annotation for ref
1 parent 1ec144b commit dce2b52

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

jscomp/syntax/src/jsx_v4.ml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ let safeTypeFromValue valueStr =
4646
if valueStr = "" || (valueStr.[0] [@doesNotRaise]) <> '_' then valueStr
4747
else "T" ^ valueStr
4848

49+
let refTypeVar loc = Typ.var ~loc "ref"
50+
4951
let refType loc =
5052
Typ.constr ~loc
51-
{loc; txt = Ldot (Ldot (Lident "ReactDOM", "Ref"), "currentDomRef")}
52-
[]
53+
{loc; txt = Ldot (Ldot (Lident "Js", "Nullable"), "t")}
54+
[refTypeVar loc]
5355

5456
type 'a children = ListLiteral of 'a | Exact of 'a
5557

@@ -279,11 +281,11 @@ let makePropsTypeParams ?(stripExplicitOption = false)
279281
(* TODO: Worth thinking how about "ref_" or "_ref" usages *)
280282
else if label = "ref" then
281283
(*
282-
If ref has a type annotation then use it, else `ReactDOM.Ref.currentDomRef.
284+
If ref has a type annotation then use it, else 'ref.
283285
For example, if JSX ppx is used for React Native, type would be different.
284286
*)
285287
match interiorType with
286-
| {ptyp_desc = Ptyp_any} -> Some (refType loc)
288+
| {ptyp_desc = Ptyp_any} -> Some (refTypeVar loc)
287289
| _ ->
288290
(* Strip explicit Js.Nullable.t in case of forwardRef *)
289291
if stripExplicitJsNullableOfRef then stripJsNullable interiorType
@@ -1077,7 +1079,14 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
10771079
(* (ref) => expr *)
10781080
let expression =
10791081
List.fold_left
1080-
(fun expr (_, pattern) -> Exp.fun_ Nolabel None pattern expr)
1082+
(fun expr (_, pattern) ->
1083+
let pattern =
1084+
match pattern.ppat_desc with
1085+
| Ppat_var {txt} when txt = "ref" ->
1086+
Pat.constraint_ pattern (refType Location.none)
1087+
| _ -> pattern
1088+
in
1089+
Exp.fun_ Nolabel None pattern expr)
10811090
expression patternsWithNolabel
10821091
in
10831092
(* ({a, b, _}: props<'a, 'b>) *)
@@ -1293,7 +1302,7 @@ let transformSignatureItem ~config item =
12931302
psig_loc
12941303
((* If there is Nolabel arg, regard the type as ref in forwardRef *)
12951304
(if !hasForwardRef then
1296-
[(true, "ref", [], Location.none, refType Location.none)]
1305+
[(true, "ref", [], Location.none, refTypeVar Location.none)]
12971306
else [])
12981307
@ namedTypeList)
12991308
in

jscomp/syntax/tests/ppx/react/expected/forwardRef.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ module V4A = {
187187
ref?: 'ref,
188188
}
189189

190-
let make = ({?className, children, _}: props<_, _, ReactDOM.Ref.currentDomRef>, ref) =>
190+
let make = ({?className, children, _}: props<_, _, 'ref>, ref: Js.Nullable.t<'ref>) =>
191191
ReactDOM.jsxs(
192192
"div",
193193
{
@@ -239,7 +239,7 @@ module V4AUncurried = {
239239
ref?: 'ref,
240240
}
241241

242-
let make = ({?className, children, _}: props<_, _, ReactDOM.Ref.currentDomRef>, ref) =>
242+
let make = ({?className, children, _}: props<_, _, 'ref>, ref: Js.Nullable.t<'ref>) =>
243243
ReactDOM.jsxs(
244244
"div",
245245
{

0 commit comments

Comments
 (0)