How to type a computed property in the new composition API? How to type a computed property in the new composition API? vue.js vue.js

How to type a computed property in the new composition API?


There are two different types for that.

If your computed prop is readonly:

const nameAndCountry: ComputedRef<string> = computed((): string => `The movie name is ${movieName.value} from ${country.value}`);

if it has a setter method:

const nameAndCountry: WritableComputedRef<string> = computed({  get(): string {    return 'somestring'  },  set(newValue: string): void {    // set something  },});