I’m new to ReasonML and also new to typed languages (just a little Flow). I’m creating a library to learn the language and I ran into a situation where the type inference works in the “Try Now” with the types all in one file. However, in my React render function, it doesn’t work, saying .
.
I would assume that if I manually type the param (completely reasonable… no pun intended), then the List.find function should infer the list elements. Am I doing something wrong or is this standard?
Example on Try Reason:
type schemaItem = { name: string, label: string };
type schemas = list(schemaItem);
let formSchema: schemas = [
{name: "name", label: "Name"},
{name: "age", label: "Age"},
];
Js.log(
List.find(x => x.name == "age", formSchema)
);
Example in my code, working with type on X (simplified)
type scItem = SF_Form.schemaItem;
type schemaList = list(SF_Form.schemaItem);
let make = (~formSchema: schemaList, _children) => {
...component,
render: _self => {
let schema =
formSchema |> List.find((x: scItem) => x.name == "firstName");
<div>(text(schema.label))</div>
},
};
Can not infer type scItem of x “The record field name can’t be found.”
type scItem = SF_Form.schemaItem;
type schemaList = list(SF_Form.schemaItem);
let make = (~formSchema: schemaList, _children) => {
...component,
render: _self => {
let schema =
formSchema |> List.find(x => x.name == "firstName");
<div>(text(schema.label))</div>
},
};
To recap, why is the type scItem
needed? couldn’t Reason infer that the type schemaList
is a list of schema items and then use that information for x
?