Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to typescript vuex getter function? #1686

Open
zl7261 opened this issue Feb 29, 2020 · 4 comments
Open

How to typescript vuex getter function? #1686

zl7261 opened this issue Feb 29, 2020 · 4 comments
Labels

Comments

@zl7261
Copy link

@zl7261 zl7261 commented Feb 29, 2020

Version

3.1.2

Reproduction link

https://github.com/zl7261/vuex_study

Steps to reproduce

The vuex getters functions can return an anonymous function that can operate state of vuex .
but how to typescript annotation function param type
if I call vuex getters like this code style

const getters = {
    getStateValue(state) {
        return (value) => {
            console.log(value)
            return state[value]
        }
    }
}
this.$store.getters.getStateValue('testMsg')

Actually, 'textMsg' is type of the Inner anonymous function param,
but if I call the getters method like this, the IDE think state is String type.

What is expected?

Can be typescript

What is actually happening?

I don't know how to use type annotation in this case.

@kiaking kiaking added the typescript label Mar 3, 2020
@F0rsaken

This comment has been minimized.

Copy link

@F0rsaken F0rsaken commented Mar 12, 2020

I think you should rather use mapGetters and overall mapXXX like here: https://vuex.vuejs.org/api/#mapgetters
but you want get typings with it.

I don't know if that's what you're looking for.

@F0rsaken

This comment has been minimized.

Copy link

@F0rsaken F0rsaken commented Mar 12, 2020

Also, try writing it like this:
this.$store.getters["getStateValue"]('testMsg')
But I'm not sure if it will help with IDE

@kiaking

This comment has been minimized.

Copy link
Member

@kiaking kiaking commented Mar 13, 2020

Hmm... @ktsn Do you know anything about this?

@ktsn

This comment has been minimized.

Copy link
Member

@ktsn ktsn commented Mar 13, 2020

but if I call the getters method like this, the IDE think state is String type.

Are you talking about IDE behavior? As the getters type is any, it accepts any type of values and state and value will be any type as you don't annotate their types. I'm not sure what your expected behavior is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.