Skip to content

Instantly share code, notes, and snippets.

@GavinRay97
Created May 13, 2020 22:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GavinRay97/29f9af2f84665e5656e8298a568f1175 to your computer and use it in GitHub Desktop.
Save GavinRay97/29f9af2f84665e5656e8298a568f1175 to your computer and use it in GitHub Desktop.
Vue 3 Composition API Typescript Component Props
import { defineComponent, computed, ref } from '@vue/composition-api'
interface User {
firstName: string
lastName: number
}
export default defineComponent({
props: {
user: {
type: Object as () => User,
required: true
}
},
setup ({ user }) {
const fullName = computed(() => `${user.firstName} ${user.lastName}`)
const message = ref('This is a message')
return {
fullName,
message
}
}
})
@Schoude
Copy link

Schoude commented Nov 5, 2020

Wow! Thanks for this tip! 👍

@simonjcarr
Copy link

error Destructuring the props will cause the value to lose reactivity

@Schoude
Copy link

Schoude commented Feb 13, 2021

Yes, that's right! But the typecasting of the prop still works even if you don't destructure the props in setup()~

@vinayakkulkarni
Copy link

FYI, for folks who want to know the rule - https://eslint.vuejs.org/rules/no-setup-props-destructure.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment