mudrz
1
why does destructuring a record with optional fields result in an error:
type myType = {
a: bool,
b?: bool,
}
let myFn2 = ({a, b}: myType) => {
a
}
my expectation would be for b
to be destructured with type option<bool>
, but instead its type is bool
this is because it’s as if such a record can have two shapes:
// shape 1
{
a: bool,
}
// shape 2
{
a: bool,
b: bool,
}
You’re only considering the latter case if you destructure it like you do:
let myFn2 = ({a, b}: myType) => {
a
}
if you want to take into consideration these two cases (b being defined or not) in the same pattern matching, you have to use ?b
:
let myFn2 = ({a, ?b}: myType) => {
a && Option.getWithDefault(b, false)
}
see playground
2 Likes
mudrz
3
thanks!
Do you know where this is documented? I tried finding any info in the docs Record | ReScript Language Manual but didn’t find anything