Hello
I have a question about the following code.
type t = {
foo: string,
bar?: string,
}
let make = (~foo, ~bar=?, ()) => { foo, ?bar }
let a = { foo: "dummy" }
let b = { foo: "dummy", bar: ?None }
compiles to:
function make(foo, bar, param) {
return {
foo: foo,
bar: bar
};
}
var a = {
foo: "dummy"
};
var b = {
foo: "dummy"
};
export {
make ,
a ,
b ,
}
Why doesn’t ?None
add a field "bar": undefined
?
function make(foo, bar, param) {
return {
foo: foo,
bar: bar
};
}
var a = {
foo: "dummy"
};
var b = {
foo: "dummy"
bar: undefined,
};
export {
make ,
a ,
b ,
}
I have a problem with this because I want to write a test on a function that returns a record with optional fields. expect(make(~foo="", ()))->toStrictEqual({ foo: "" })
. But the deep equality doesn’t work : the function returns a property value undefined in the record.
The only way around this problem is to write:
type t = {
foo: string,
bar: option<string>,
}
let make = (~foo, ~bar=?, ()) => { foo, bar }
let a = { foo: "dummy", bar: None }
Have you ever experienced this problem?
Thank you for your help
Léo