This is my vue-shims.d.ts where I add the type for a global property I've added.
import { ComponentCustomProperties } from "vue";
import { CONSTANTS } from "@constants";
declare module "@vue/runtime-core" {
interface ComponentCustomProperties {
$const: typeof CONSTANTS;
}
}
This works fine until I add a data option to my component, see screen shots of with and without:


When I add the data option, Vetur thinks $const does not exist on "this", as if what's in data overwrites my augmented ComponentCustomProperties.
Typescript is still getting the type inference as you can see from the cropped off bit of the image with the error so I think this is just a vetur issue.
If you can repro in typescript file?
Sure I'll do that asap.
I've found a workaround for now, annotating return type of the computed property removed the error but threw a line 0 "cannot read property 'map' of undefined" in eslint. Upgraded @typescript-eslint/eslint-plugin and @typescript-eslint/parser to the latest version but I had to disable the @typescript-eslint/camelcase rule. It seems to have fixed the issue.
@yoyo930021
I have made a repro that I believe should work (but doesn't). You can find the repro here.
Relevant code:
// src/main.ts
import { createApp } from 'vue'
import App from './App.vue'
interface Constants {
test1: number;
test2: number;
}
const CONSTANTS: Constants = {
"test1": 1,
"test2": 2
}
let app = createApp(App)
app.config.globalProperties.$const = CONSTANTS;
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$const: Constants;
}
}
app.mount('#app')
// src/components/HelloWorld.vue
import { defineComponent } from "vue";
export default defineComponent({
name: "HelloWorld",
setup() {
let constants = this.$const; // Error: Property '$const' does not exist on type 'void'
}
});
@holwech @nrgnrg
I think it is a bug in vue 3 typescript type def.
You can open a issue to vue-next project.
https://vuejs.org/v2/guide/typescript.html#Annotating-Return-Types
Error: Property '$const' does not exist on type 'void'
setup doesn't have this, use provides instead.
@KaelWD you're right.
Makes sense, I found a SO post with some potential solutions for anyone who finds this issue via Google.
The original issue has nothing to do with using setup so I think this is still a bug.
The original issue has nothing to do with using setup so I think this is still a bug.
It is same as https://vuejs.org/v2/guide/typescript.html#Annotating-Return-Types
Most helpful comment
https://vuejs.org/v2/guide/typescript.html#Annotating-Return-Types
setupdoesn't havethis, use provides instead.