vuejs / core

🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
https://vuejs.org/
MIT License
47.89k stars 8.36k forks source link

typing defineComponent props #3796

Open Zclhlmgqzc opened 3 years ago

Zclhlmgqzc commented 3 years ago

Version

3.0.11

Reproduction link

https://github.com/youzan/vant/issues/8701

Steps to reproduce

import { NavBar } from 'vant'
import { defineComponent, h } from 'vue'

export default defineComponent({
  name: 'MyNavBar',
  props: { 
    ...NavBar.props,  
    // more props ...
  },
  setup(props, { attrs, slots }) {
    return ()  => h(
      NavBar,
      {
        ...props,
        ...attrs
      },
      slots
    )
  }
})
(JSX attribute) title?: string | undefined
<van-nav-bar title={} />

but my-nav-bar title type lost
<my-nav-bar title={} />

What is expected?

(JSX attribute) title?: string | undefined
<my-nav-bar title={} />

DGP9`LHK1JFL3E_{0XN~L4D

` B~M492 30T2AC$TJ4 }24

What is actually happening?

my-nav-bar title type lost
<my-nav-bar title={} />
posva commented 3 years ago

You might want to open a PR. But you will need to explain yourself (in English) Ines off just post pictures

Zclhlmgqzc commented 3 years ago

@posva I added some background information

chenjiahan commented 3 years ago

@posva @pikax The type of Component.props is missing after wrapping the defineComponent method, which makes it difficult to create HOC.

import { defineComponent } from 'vue';

const Component1 = {
  props: {
    foo: String,
  },
};

const Component2 = defineComponent(Component1);

Component1.props; // --> { foo: StringConstructor }
Component2.props; // --> any
yyx990803 commented 2 years ago

5416 introduced a regression because it changed the argument order of DefineComponent, which breaks component definitions generated via tsc/vue-tsc, and a number of other cases.

We need to find a way to do it while preserving the current argument order of DefineComponent.

productdevbook commented 1 year ago

@yyx990803

There must be a way to send type, attrs only takes data, but if we define interface, at least volar can collect both props and attrs types. This feature opens it to libraries like the read-ui.

Couldn't it be easy to send attrs an interface or type as we can write props in a simple way?