arktype icon indicating copy to clipboard operation
arktype copied to clipboard

type reference inferred without ` | undefined` without `exactOptionalPropertyTypes`

Open flodlc opened this issue 9 months ago • 6 comments

Report a bug

🔎 Search Terms

undefined, optional

🧩 Context

  • ArkType version: 2.0.4
  • TypeScript version (5.1+): 5.7.3
  • Other context you think may be relevant (JS flavor, OS, etc.):

🧑‍💻 Repro

It seems like an obvious bug so maybe it comes from a wrong usage. When I reuse a property type that can be undefined, it looses it's undefined type.

const age = type("number | undefined");

const User = type({
  age: age,
});

const user = user({ age: undefined });
// user.age type is number, not mentions undefined.

flodlc avatar Feb 14 '25 19:02 flodlc

I can't reproduce this:

Image

If you're still running into this issue locally, could you add a full repro?

ssalbdivad avatar Feb 14 '25 20:02 ssalbdivad

We definitely don't have the same result. I will try in a minimal repo. Could you send me your tsconfig ? Image

flodlc avatar Feb 14 '25 20:02 flodlc

It looks like this occurs with "exactOptionalPropertyTypes": false.

The docs recommend setting that flag to true, but undefined should not be stripped here regardless.

Will take a look and see if we can fix for the next release.

ssalbdivad avatar Feb 14 '25 20:02 ssalbdivad

Yes, I just got it too, setting "exactOptionalPropertyTypes": true makes it work. Thanks for the very fast support and for your work.

flodlc avatar Feb 14 '25 20:02 flodlc

The docs recommend setting that flag to true, but undefined should not be stripped here regardless.

Yes agree with this, it's actually not so obvious to set exactOptionalPropertyTypes to true in many projects.

flodlc avatar Feb 14 '25 21:02 flodlc

Won't be able to address this right away in 2.1.0 since the immediate solutions I found had significant performance overhead, but will try and find something that works well as a followup.

ssalbdivad avatar Feb 14 '25 21:02 ssalbdivad