Issue with recognizing global methods in Vue and Typescript – help needed

I have a Vue mixin that looks like this:

const languageMixin = Vue.extend({
    methods: {
        $getLanguages: function(): object {
            return {
                en: 'english'
            }
        }
    }
}


Vue.mixin(languageMixin)
new Vue({
    router,
    render: h => h(Frame)
}).$mount('#app')

... and I'm attempting to utilize it in a component:

<template lang="pug">
    .languages
        a( v-for="language, code in $getLanguages()" :key="code" @click="$root.$i18n.locale = code") {{ language }}&nbsp;
</template>

<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
    methods: {
        languagesList: function () {
            console.log(this.$getLanguages)
        }
    }
})
</script>

However, I encounter an error message that reads "Property '$getLanguages' does not exist on type 'CombinedVueInstance>>'"

Interestingly, the function works when used in the template as the language names are displayed. It's just the TypeScript code that does not recognize the function.

Answer №1

If you want to enhance the vue module by adding a typing for $languages, consider the following:

// vue-shim.d.ts

declare module 'vue/types/vue' {
  interface Vue {
    $languages: LanguageService
  }
}

Here's an example of a psuedo LanguageService:

// language-service.d.ts

export interface LanguageService {
  $languages: () => Record<string, string>
}

To learn more about how to augment Vue, check out this resource.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am experiencing issues with my HTML select list not functioning properly when utilizing a POST service

When using Angularjs to automatically populate a list with *ngFor and then implementing a POST service, the list stops functioning properly and only displays the default option. <select id="descripSel" (change)="selectDescrip()" > <option >S ...

Understanding Different Symbols in TypeScript

Can you explain the purpose of symbols in TypeScript to me? As someone familiar with Java, it seems a bit unnecessary to use them alongside an interface declaration. What is the reason for setting symbols in TypeScript? For example, consider the followin ...

Customize the data attributes of Vuetify v-select options for added functionality

I am currently utilizing a v-autocomplete component to display items in a selector. Given that v-autocomplete is essentially just an enhanced version of v-select, I believe a solution for v-select would be applicable for v-autocomplete as well. Within my ...

Collection of functions featuring specific data types

I'm currently exploring the idea of composing functions in a way that allows me to specify names, input types, and return types, and then access them from a central function. However, I've encountered an issue where I lose typing information when ...

Why is TypeScript unable to recognize package exports? (using CommonJS as the module system and Node as the module resolution)

I have an NPM package that is built for ESM and CJS formats. The package has a dist folder in the root directory, which contains: dist/esm - modules with ESM dist/cjs - modules with CJS dist/types - typings for all modules In the package.json file, there ...

Setting the font size for the entire body of a webpage globally is ineffective

Technology Stack: Nuxt.js + Vuetify.js Problem: Unable to set global body font size Solution Attempt: I tried to adjust the body font size to 40px in ~/assets/style/app.styl: // Import Vuetify styling ...

Anyone have any suggestions on how to resolve the issue with vertical tabs in material UI while using react.js?

I'm working on integrating a vertical tab using material UI in react.js, but I'm facing an issue where the tabs are not appearing. Here is the snippet of my code: Javascript: const [value, setValue] = useState(0); const handleChange1 = (event ...

How come this constant can be accessed before it has even been declared?

I find it fascinating that I can use this constant even before declaring it. The code below is functioning perfectly: import { relations } from 'drizzle-orm' import { index, integer, pgTable, serial, uniqueIndex, varchar } from 'drizzle-orm ...

What sets declaring variables apart in Vue?

Can you clarify the distinctions among various methods of declaring variables? When should I employ each of these declaration methods? <script> const someVariable = 12345 export default { name: 'App', } </script> <script> e ...

Tips for concealing tick labels in d3 using TypeScript

When trying to hide tick labels by passing an empty string to the .tickFormat("") method, I encountered an issue with Typescript. The error message received was as follows: TS2769: No overload matches this call. Overload 1 of 3, '(format: null): Axi ...

Having trouble with React throwing a SyntaxError for an unexpected token?

Error message: Syntax error: D:/file/repo/webpage/react_demo/src/App.js: Unexpected token (34:5) 32 | 33 | return ( > 34 <> | ^ 35 <div className="status">{status}</div> 36 <div className=&quo ...

What is the best way to prevent users from entering a zero in the first position of a text box using JavaScript

Although I am aware this may be a duplicate issue, the existing solution does not seem to work for me. The field should accept values like: valid - 123,33.00, 100,897,99, 8000 10334 9800,564,88.36 invalid - 001, 0 ...

Exploring TypeScript implementation of Redux toolkit's store

I'm currently diving into the world of Redux in React + TypeScript by following the tutorials provided by Redux Toolkit. I am attempting to implement it in a sample application. My main struggle lies with typings related to the store and the mappStat ...

What causes the undefined value of "this" in the Vue Composition API setup function?

A Vue component I've created is using v3's composition API: <template> <input type="checkbox" v-model="playing" id="playing" @input="$emit('play', $event.target.value)" /> <labe ...

What is the best way to access a private class variable within the sockent.on function in Angular?

export class AppComponent { title = 'my-app'; constructor(private notifyService : NotificationService) {} ngOnInit() { socket.on("laravel_database_chat:test", function(message){ //I AM ATTEMPTING TO INVOKE THE NOTIF ...

Executing asynchronous callback with Electron and Vue using nodeffi

Currently, I am experimenting with using Async along with a received Buffer. While I am able to obtain the correct answer, I am facing challenges in accessing variables outside of the callback function. Specifically, I am attempting to assign the value o ...

The focus is lost when v-if is triggered during input

Within my code, there is an input field: <input type="text" maxlength="2" :value="valueHours" @input="calculateTimes($event)" @blur="updateValueHours($event)"> Accompanied by a list section: <div ...

CompositeAPI: Referencing HTML Object Template - Error TS2339 and TS2533 when using .value to access Proxy Object

Having trouble referencing an element in VueJS 3 CompositeAPI. In my current implementation, it looks like this: <div ref="myIdentifier"></div> setup() { const myIdentifier = ref(null); onMounted(() => { console.log(myIden ...

Read PDF on Nuxt site

I am working on a Nuxt app and I need to display a PDF file containing the GDPR policy on a specific page, such as /gdpr. I attempted to place the PDF in the static folder, but accessing it through a URL like /gdpr.pdf did not work. I have been unable to ...

Exploring the contrast of && and ?? in JavaScript

My current focus is on utilizing the Logical AND && and Nullish coalescing operator ?? in handling conditional rendering of variables and values. However, I find myself struggling to fully comprehend how these operators function. I am seeking clar ...