Skip to content

Commit c5c2e4e

Browse files
gabeleviFacebook Github Bot
authored andcommitted
Seal the JSX attributes object if there is no spread
Summary: Currently, if you try to use exact objects for react props, you get errors about flowing inexact objects to exact objects. This diff does NOT fix that. To fix that, we probably need to fiddle a bunch more with the complicated react machinery. However, the `jsx` pragma code is much more simple. So by sealing this object, we allow the jsx function to expect an exact object type. See the test for an example. Reviewed By: samwgoldman Differential Revision: D4015376 fbshipit-source-id: 9810691c1fe4601f1c0d19e10c5c75b764310e2d
1 parent d0f8787 commit c5c2e4e

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

newtests/jsx_pragma/test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,34 @@ export default suite(({addFile, addFiles, addCode}) => [
272272
^^^^^^^^^^^^^^ JSX element \`Bar\`. Could not resolve name
273273
`,
274274
),
275-
])
275+
]),
276+
test('Exact prop type without spread should work', [
277+
addCode(`
278+
// @jsx Foo
279+
function Foo(elem: number, props: {| x: string |}) {}
280+
const Bar = 123;
281+
282+
<Bar x="hi" />;
283+
`).noNewErrors(),
284+
]),
285+
test('Exact prop type with spread still doesnt work', [
286+
addCode(`
287+
// @jsx Foo
288+
function Foo(elem: number, props: {| x: string |}) {}
289+
const Bar = 123;
290+
291+
const props = {x: "hi"};
292+
<Bar {...props} />;
293+
`).newErrors(
294+
`
295+
test.js:9
296+
9: <Bar {...props} />;
297+
^^^^^^^^^^^^^^^^^^ JSX desugared to \`Foo(...)\`
298+
9: <Bar {...props} />;
299+
^^^^^ spread of object literal. Inexact type is incompatible with exact type
300+
5: function Foo(elem: number, props: {| x: string |}) {}
301+
^^^^^^^^^^^^^^^ exact type: object type
302+
`,
303+
),
304+
]),
276305
]);

src/typing/statement.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3496,7 +3496,7 @@ and jsx_title cx openingElement children = Ast.JSX.(
34963496
let reason_props = replace_reason_const
34973497
(if is_react then RReactElementProps name else RJSXElementProps name)
34983498
reason in
3499-
let o = Flow.mk_object_with_map_proto cx reason_props
3499+
let o = Flow.mk_object_with_map_proto cx reason_props ~sealed:(!spread=None)
35003500
!map (MixedT (reason_props, Mixed_everything))
35013501
in
35023502
match !spread with

0 commit comments

Comments
 (0)