unplugin icon indicating copy to clipboard operation
unplugin copied to clipboard

Inexact optional property types

Open iwoplaza opened this issue 8 months ago • 3 comments

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?

iwoplaza avatar Apr 11 '25 15:04 iwoplaza

/cc @antfu WDYT

sxzz avatar Apr 12 '25 11:04 sxzz

Bumping this for visibility 👀

iwoplaza avatar May 15 '25 09:05 iwoplaza

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.

antfu avatar May 16 '25 00:05 antfu

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

iwoplaza avatar Jun 19 '25 17:06 iwoplaza