Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defaulted optional to undefined does not fill field with default value #1185

Open
yannbriancon opened this issue Jun 12, 2023 · 0 comments
Open

Comments

@yannbriancon
Copy link

yannbriancon commented Jun 12, 2023

[BUG]

Description

Using defaulted with optional type and undefined 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.

const UserInfo = type({
  name: string(),
  age: defaulted(optional(number()), undefined)
});

const user = create(
  {
    name: "3"
  },
  UserInfo
);

/*
  * user = { 
  *     name: "3"
  *     age: undefined
  * }
  */

Actual Behaviour

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
  * }
  */

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 for undefined type without being optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant