vuejs / vue-class-component

ES / TypeScript decorator for class-style Vue components.
MIT License
5.81k stars 429 forks source link

Obsolete prop docs? #414

Closed AlvaroBrey closed 4 years ago

AlvaroBrey commented 4 years ago

The documentation suggests using Vue.extend to define props. However, defining the props in the decorator options and using PropType seems to work, and is more concise:

<template>
<div>
  {{ message }}
</div>
</template>

<script lang="ts">
import Vue, { PropType } from 'vue'
import Component from 'vue-class-component'

interface FooInterface {
  bar: string;
  baz: number;
}

@Component({
  props: {
    testProp: {
      type: Object as PropType<FooInterface>
    }
  }
})
export default class UserImportResultsTable extends Vue {
  get message(){
    return `${testProp.bar} ${testProp.baz.toFixed()}` // type checking works
  }
}
</script>

Is there any reason why this is not recommended, or is the documentation just outdated (from when PropType didn't exist) ?

ktsn commented 4 years ago

We state all options are valid in @Component decorator factory function. https://class-component.vuejs.org/guide/class-component.html#other-options

Where you mention is additional guide for TypeScript as decorator does not infer the type from decorator and props are unavailable on type level unless you explicitly declare it in class definition. If you prefer the explicit definition it's totally ok.

djeikyb commented 4 years ago

This was also unclear to me. The only place props is documented specifies this style of Vue.extend. I only found the @Component style because of this github issue. The docs were confusing, which is my cue to trawl github issues. It looks like there's also @Props(), which I found on accident with ide intellisense.

It'd be helpful to document these possibilities, maybe also a recommendation for preferred style?

AlvaroBrey commented 4 years ago

@djeikyb @Prop is provided by vue-property-decorator, not by this lib.

djeikyb commented 4 years ago

Oh wow I didn't notice that it came from a repo outside the vuejs org.. I just started a typescript vue project with the vue cli tool, which I guess it comes with vue-property-decorator. I understand now why it's not mentioned in the vue-class-component docs!