Subscribe on changes!

(typescript) Required prop can be undefined when props is declared outside `defineComponent`

avatar
Jan 14th 2021

Version

3.0.5

Reproduction link

https://codesandbox.io/s/loving-frost-l1gox?file=/src/index.ts

Steps to reproduce

follow the link

image

What is expected?

props.requiredValue shouldn't be inferred as undefined.

What is actually happening?

Opposite

avatar
Jan 14th 2021

When declaring it outside you need as const:

const props = {
  requiredValue: {
    type: Array as PropType<string[]>,
    required: true
  }
} as const;
avatar
Jan 14th 2021

thanks!

avatar
Apr 26th 2021

Hi, sorry to comment this closed issue but I can't find a way to send props to a custom defineComponent function, e.g. :

import { defineComponent } from 'vue'

export const props = {
  msg: {
    type: String,
    required: true
  }
} as const

function customComponent(props: {}) {
  return defineComponent({
    props
  })
}

export default customComponent(props)

Is there any way to do that easily without using a custom DefineComponent type (I tried but without success for the moment) ?

avatar
Apr 26th 2021

Well, it was not so difficult (thanks to pikax on discord) :

function customComponent<P extends Readonly<ComponentPropsOptions> = {}>(
  props: P
) {
  return defineComponent({
    extends: Parent,
    props
  })
}
avatar
May 10th 2021

@klevron what is Parent ?