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

optional handling in object #3

Closed
jordan-boyer opened this issue Mar 2, 2023 · 4 comments · Fixed by #5
Closed

optional handling in object #3

jordan-boyer opened this issue Mar 2, 2023 · 4 comments · Fixed by #5

Comments

@jordan-boyer
Copy link
Contributor

given the following snippet

const person = object({
    name: string().or(optional()),
    age: number(),
  });

type Person = Infer<typeof person>;

we have the resulting person type

type Person = {
    name: string | undefined;
    age: number;
}

where we could have

type Person = {
    name?: string | undefined;
    age: number;
}

what do you think ?

@adversinc
Copy link

adversinc commented Mar 2, 2023

I second that. Infer<> does not generate the optional parameters so the following code results an error:

const person = object({
    name: string().or(optional()),
    age: number().or(optional()),
  });

type Person = Infer<typeof person>;

function updatePerson(personData: Person = {}) { ... }

It would be great to get it fixed.

@thoughtspile
Copy link
Owner

thoughtspile commented Mar 2, 2023

Ouch, looks like superstruct typetest suite has failed me:

test<{
a?: number | undefined
b: string
}>(object({
a: number().or(optional()),
b: string(),
}))

The inferred type is assignable to the target one, but they're not equal.

@jordan-boyer
Copy link
Contributor Author

If you want a better test you can add this "exactOptionalPropertyTypes": true and write the test like this:

test<{
  a?: number
  b: string
}>(object({
  a: number().or(optional()),
  b: string(),
}))

which typescript complain without a proper fix for the optional on object

jordan-boyer added a commit to jordan-boyer/banditypes that referenced this issue Mar 3, 2023
also add tsafe for testing that two types are equals

closes thoughtspile#3
jordan-boyer added a commit to jordan-boyer/banditypes that referenced this issue Mar 3, 2023
also add tsafe for testing that two types are equals

closes thoughtspile#3
jordan-boyer added a commit to jordan-boyer/banditypes that referenced this issue Mar 3, 2023
also add tsafe for testing that two types are equals

closes thoughtspile#3
@thoughtspile
Copy link
Owner

Fixed in 0.2.3

I also updated the typetest suite for strict checks, and opened an issue in superstruct in case they'd like to avoid problems like these.

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

Successfully merging a pull request may close this issue.

3 participants