superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

optional() result on returning null after object creation

Open aldipower opened this issue 2 years ago • 1 comments

Given this struct:

const User = object({
  userId: string(),
  description: optional(string()),
});

creating an object without a description

const user = create({ userId }, User);

results in an object with a description of null

{
  userId: "sadfsdfxcbvxd",
  description: null,
}

I would expect undefined here for the description. null adds actually a value and changes the structure of the object by adding a key.

Is this intentional? This feels like a bug to me.

aldipower avatar Dec 01 '21 15:12 aldipower

@aldipower For me your use case returns undefined, see example.

However, I would not expect the property to be set undefined when the input object doesn't own the property at all.

yss14 avatar Mar 01 '22 14:03 yss14