-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
App fails at runtime even though TS and Volar are happy with generic defineComponent Props type #7832
Comments
In Vue, props always require runtime config. So what you are trying to do is not supported. In |
@LinusBorg I had script setup's defineProps in mind when I tried defineComponent's generic type. I was hoping that defineComponent's generic Props type might be processed the same way as defineProp's generic type. If that's not possible for some reason, would this be better suited as a feature request to make props runtime validation optional? I truly don't care about that "feature" if the tooling is giving me immediate type validation. At the very least, if the goal is to require a runtime props definition, I would still consider it a bug that I'm able to set the Props type in the type parameter without any error. At the very least there should be a TS error expecting the passed object to have a props property matching the type passed as the generic type parameter. |
Also, @LinusBorg I decided to look into the // overload 2: object format with no props
// (uses user defined props interface)
// return type is for Vetur and TSX support
export function defineComponent<
Props = {},
RawBindings = {},
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string
>(
options: ComponentOptionsWithoutProps<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
I,
II
>
): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE> |
We would have to ask Evan what the exact use case here is. the line about "Vetur and TSX" (we not have Volar as the default IDE extension) sounds like this type is overload is being used by tooling when converting Vue SFCs into a TSX-like representation for TSC support. I could be wrong though. At the runtime level, there is definitely no support for this whatsoever. When a component has no runtime props definition, all props passed to it end up in There has been some discussion in our RFCs repo about allowing something like that a while ago, but there has been no activity lately: |
@LinusBorg Thanks for the RFC link - that does actually look like exactly what would help here. I think the TS experience with the Options API would be hugely improved if users could type Props and Emits without the runtime validation. |
Vue version
3.2.47
Link to minimal reproduction
https://sfc.vuejs.org/#eNqNUsuO1DAQ/JXGl9mVJrG4RmEkxAUuCGkRJ1+8SWfiVWIbt7NLFOXfaecxWRYhccijbXdXuaom8dH7/HlAUYiSqmB8hE7b6wclIilxUVZF03sXInxyvYcmuB5OuUxFajspC5Ae/LUcqrHRQxdhSmsAFR9zFm2kYl+CZdB5Leb04VcpV+yEZ8uIve90RK4AyndZBj9cpwNPCwGr2I1ArXsh0BYwBBegxYBgGiDX47fgPBgC6yJ4TYQ1ZNkx6Q6f0cKLia0btjuRx8o0o7FXCIPl6yJ4HkL3t8blWJGmZ2mHxfnsXNCjEvKY/H30+LAqqG29UdZMrNXej6CbiGFhlIB2pmd4ZBo7bKNNRytqKV+pIM5iNSHrtc+fyFm2a9FTbRvs1U1hJdiYVCvRxuipkJKaKrn1RLkLV8l/+QaZI/XZY2A1MfBgJTZDZobcPf5nNAC2aEzJd2MxdSx2w7wl5XVEpISHQ+rYIiQFCCILB7VDsieWAn8OhkXTf3rBABXrqaNxnJC/4vYGvpxuAhdAMSS8+XI3zfcsD9P46lbQIz3/G8HURkfONIdwB0hjzmnly6lO/DiptwC17y8HoelgB/NcSt586/f8GwO4PA8=
Steps to reproduce
App fails at runtime even though TypeScript and Volar are happy with defineComponent Props type.
App.vue
Comp.vue
What is expected?
Initially from reading the Vue docs, I expected to see a type error about not specifying the runtime props in the object passed to
defineComponent
along the lines of "Type undefined not assignable to (whatever my prop types were)".BUT when I realized that TypeScript was happy AND that Volar was correctly seeing the prop type in the template AND that Volar was seeing the prop as required in the parent component's template...I started to think that this would "just work" even in runtime.
What is actually happening?
Sadly the app still failed at runtime with the error
But then I thought...why does that need to fail? Ultimately I don't care about the runtime validation if TypeScript and Volar are already validating the props as I'm writing the code. Now runtime validation of component props feels like trying to do runtime validation of function params - we don't really need it.
System Info
Any additional comments?
As a side note, part of my motivation for trying the generic Props type parameter in the first place is that I hate the idea of using JS prototypes just to accomplish all I really care about, which is type checking.
I would ALSO love if defineComponent's "Emits" generic type parameter could be second in the list of type parameters instead of way later...because if I'm able to specify both the props and the emits types right there in
defineComponents
(and if I can opt out of the runtime validation for both), then 💯 🎉The text was updated successfully, but these errors were encountered: