Skip to content
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

non defined props in functional components are empty #5469

Closed
enpitsuLin opened this issue Feb 22, 2022 · 6 comments
Closed

non defined props in functional components are empty #5469

enpitsuLin opened this issue Feb 22, 2022 · 6 comments

Comments

@enpitsuLin
Copy link

enpitsuLin commented Feb 22, 2022

Version

3.2.25

Reproduction link

codesandbox.io

Steps to reproduce

See reproduction link

What is expected?

Both components Test1 and Test2 behave consistently

What is actually happening?

The Test1 component is written in such a way that the props defined by the generic parameters are missing(BTW my IDE code hints work fine), in fact the option of the component has no props option, and the additional definition of props like the following will make it work correctly.

import { defineComponent, ref } from "vue";

const HelloWorld = defineComponent<{ msg: string }>(({ msg }) => {
  // some code
});

HelloWorld.props = { msg: { type: String } };

export default HelloWorld;

Does this mean that defineComponent requires adding additional options such as props? Or is this a working feature?


I just want to make it easier to use JSX instead of SFC, and more functional, maybe there are some compile-time plugins here that can make this write work for me?

@posva posva changed the title defineComponent overload 1 does not provided component props correctly non defined props in functional components are empty Feb 22, 2022
@posva posva added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. 🐞 bug Something isn't working has workaround A workaround has been found to avoid the problem labels Feb 22, 2022
@posva
Copy link
Member

posva commented Feb 22, 2022

The bug is that props should contain the attrs when no props option is defined. As a workaround, define the props option

@LinusBorg
Copy link
Member

LinusBorg commented Feb 22, 2022

@posva that only applies to functional components, doesn't it?

Vue requires runtime props options from which prop types are inferred. It's currently not possible to define a (stateful) component without runtime props and then provide prop types via the generic argument.

I started a discussion some Time ago, @pikax also wrote a small proposal about how to support this in the RFCs repo I think, but so gar it went nowhere.

@sqal
Copy link
Contributor

sqal commented Feb 22, 2022

Is it really a bug @posva? I've never used defineComponent, but it looks to me like it's just a helper function for defining a stateful components (with type inference) and those requires props to be declared upfront. Not sure why author mentioned functional component here.

I think with TypeScript it's better to use script-setup and defineProps which will generate runtime props definition for you.

https://vuejs.org/guide/typescript/composition-api.html#typing-component-props

@enpitsuLin
Copy link
Author

@sqal script-setup are excellent, but I can't use both render function and script-setup

@enpitsuLin
Copy link
Author

thanks lot, I will follow #3102 and this rfc , maybe these can solve my problem

@posva posva removed 🐞 bug Something isn't working 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem labels Feb 23, 2022
@posva
Copy link
Member

posva commented Feb 23, 2022

It's not a bug, I somehow read a functional component but it's was a regular one... This is indeed expected for stateful components

@github-actions github-actions bot locked and limited conversation to collaborators Oct 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants