Search code examples
typescriptpluginsvuejs3vue-composition-api

How to type Vue3 plugin function for Typescript - error is of type 'unknown'


I have a plugin with some functions inside.

export default {
    install: (app:any) => {

        app.provide('translate', (translations:Array<Object>, key:string) => {

            let translation = translations.find((item:any) => {
                return item[key] && item.locale === lang
            });
            .......

            return translation

        })

Then when I want to use it in component via inject()

const translate = inject('translate')

I am getting an error TS18046: 'translate' is of type 'unknown'.

I dont understand what should I set up for this case.

Thanks a lot for any help.


Solution

  • So it seems the solution is to write

    const translate = inject<Function>('translate')
    

    but this solution lead to another error which says Cannot invoke an object which is possibly 'undefined' as a function Solution is to use composables instead of plugin.