Inexact optional property types
Environment
- unplugin: 6.2.2
- typescript: 5.7.2
Reproduction
https://stackblitz.com/edit/vitejs-vite-lovdzaug?file=unplugin-foo.ts
Describe the bug
Definitions like:
type StringFilter = FilterPattern | {
include?: FilterPattern
exclude?: FilterPattern
};
... can cause friction when using the TypeScript exactOptionalPropertyTypes compiler option, since under that configuration, undefined is not a valid value to be passed into include or exclude. Would it break anything to widen the type to the following?:
type StringFilter = FilterPattern | {
include?: FilterPattern | undefined
exclude?: FilterPattern | undefined
};
Additional information
- [x] Would you be willing to help implement a patch for this?
/cc @antfu WDYT
Bumping this for visibility 👀
I would need more explanations about why exactOptionalPropertyTypes is useful and what benefit it provides. As the option is not enabled by default in TypeScript, I believe the majority of the packages on npm are not following this pattern, which makes this change feel a bit less effective.
Sorry for the late response. I was going to base my argument on "optimizing for strictness makes it compatible for both strict and non-strict setups", which is still true, but TypeScript recently made exactOptionalPropertyTypes true by default when running tsc --init: https://github.com/microsoft/TypeScript/pull/61813
Still, typing everything explicitly as foo?: T | undefined is interpreted the same in a "strict" and a "non-strict" setup, while foo? T is interpreted differently based on the end user's tsconfig