Subscribe on changes!

Types: withDefault narrows defineProperties types

avatar
Aug 4th 2021

Version

3.2.0-beta.7

Reproduction link

https://jsfiddle.net/7cxadqbh/

Steps to reproduce

See https://github.com/johnsoncodehk/volar/issues/344 for full context / discussion.

Basically, when you defineProps of type any and provide a withDefault, the component property is typed with the withDefault type, which is a narrower type than the original prop type.

In the provided repro, an any prop becomes never[] | undefined, which is not usable.

What is expected?

If I define an optional prop of type P, and provide a default value of type D, I expect:

  • The returned props variable should be typed as P -- or maybe P extends undefined ? never : P, aka P with undefined removed, since we provide a default value. This works ok 👍
  • The component prop should be typed as P | undefined.

What is actually happening?

At least in the repro (which involves the special type any, not sure if that's relevant to the bug or not), it seems that the component property is D | undefined.