vue: [Vue + Typescript] Property does not exist on type in computed function
Version
2.5.17
Reproduction link
https://codesandbox.io/s/10l5yrl9rq
Steps to reproduce
Codesandbox seems not support typescript code intelligense in *.vue, the file is src/index.vue
Here is the code:
<script lang="ts">
import Vue from 'vue'
interface TestInterface {
a: string
}
export default Vue.extend({
props: {
prop1: {
type: Object as () => TestInterface,
},
},
computed: {
computed1() {
return this.prop1.a
}
}
})
</script>
What is expected?
Typescript compiler will not complaint
What is actually happening?
Property ‘prop1’ does not exist on type ‘{ computed1(): any; }’.
Is there any way to solve this problem?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 21 (2 by maintainers)
Let me left a note for everyone who’re confused as i am: annotate ALL return types of computed properties.
Please read https://vuejs.org/v2/guide/typescript.html You should do the following things to make type inference works.
strict: trueornoImplicitThis: truefor compiler option.Pretty sure he meant that one of its computed properties was not explicitly typed.
Sorry for the necro, but I am still getting this error when the computed property is not a function, but rather a get+set construct like this:
I am using the latest vue-cli typescript setup (just set it up this week), strict mode is enabled and I think I got the return types covered. Is there anything I’m missing? Thanks!
Edit: It looks like I had a single computed property that was not documented, which caused this error. Seems to work now.
I had the same issue
Property 'sender' does not exist on type '(() => any) | ComputedOptions<any>'. Property 'sender' does not exist on type '() => any'.Vetur(2339)My code was like:
✅ In order to solve this issue, I just added a return type on each computed variable.
This also happens on methods, vscode can find those at runtime, but at compile time it gives error in every ‘this’ used inside a method.
What does “having a single computed property that was not documented” means ?
I had the same issue, and I know this issue has been closed for some time now but it could maybe help people who are also just starting with TypeScript and Vue 😃
My issue was that I got the error “Property ‘value’ does not exist on type ‘(() => any) | ComputedOptions<any>’. Property ‘value’ does not exist on type ‘() => any’.ts(2339)” for
return this.valuein the computed get ofcomputedValue:In order to resolve my issue, I added a type to the parameter of the computed set function:
Hope this helps for other people 😃 Have a great day!
Don’t really remember what I did, we changed the project to not use typescript and class type components, we are using now vue 2.6 with vue composition and javascript.
@GustavoFenilli how did you fix that? right now vue on typescript is giving me more pain than it’s worth. All my return types are already annotated.
Edit: I already checked the forum and found this question asked in 2018 with 0 answers. The poster just worked around it by casting
(this as any).