Open yannbriancon opened 1 year ago
[BUG]
Using defaulted with optional type and undefined default value creates an object without default field.
defaulted
optional
undefined
Issue comes between version 0.16.0 and version 0.16.2, almost same time another issue was fixed about optional (#1106, @cwouam)
Defaulted optional field exists with default value to undefined.
const UserInfo = type({ name: string(), age: defaulted(optional(number()), undefined) }); const user = create( { name: "3" }, UserInfo ); /* * user = { * name: "3" * age: undefined * } */
Defaulted optional field does not exist.
const UserInfo = type({ name: string(), age: defaulted(optional(number()), undefined) }); const user = create( { name: "3" }, UserInfo ); /* * user = { * name: "3" * // missing age field * } */
https://codesandbox.io/s/superstruct-optional-defaulted-bug-p79txr?file=/src/index.ts:478-579
I think we could modify the behaviour to make defaulted of an optional convert to a required property.
Another way to change this without breaking would be to add another type undefined to allow for undefined type without being optional.
[BUG]
Description
Using
defaulted
withoptional
type andundefined
default value creates an object without default field.Issue comes between version 0.16.0 and version 0.16.2, almost same time another issue was fixed about
optional
(#1106, @cwouam)Expected Behaviour
Defaulted optional field exists with default value to undefined.
Actual Behaviour
Defaulted optional field does not exist.
Reproduction
https://codesandbox.io/s/superstruct-optional-defaulted-bug-p79txr?file=/src/index.ts:478-579
Opinion
I think we could modify the behaviour to make defaulted of an optional convert to a required property.
Another way to change this without breaking would be to add another type
undefined
to allow forundefined
type without beingoptional
.