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

Property 'auth' has no initializer and is not definitely assigned in the constructor #517

Open
notflip opened this issue Oct 23, 2021 · 1 comment

Comments

@notflip
Copy link

notflip commented Oct 23, 2021

I would like to use data from the auth module, in the gallery module (to get the current signed in user)
However TS is complaining about the auth variable inside my GalleryActions class
TS2564: Property 'auth' has no initializer and is not definitely assigned in the constructor.

Am I missing something? Thank you.

auth module

class AuthState {
    user?: User
    authenticationError?: string
}

export const auth = new Module({
    state: AuthState,
    getters: AuthGetters,
    mutations: AuthMutations,
    actions: AuthActions
})

gallery module

import {auth as authModule} from './auth'

class GalleryActions extends Actions<GalleryState, GalleryGetters, GalleryMutations, GalleryActions> {

    auth: Context<typeof authModule> // <--------------------- errored line

    $init(store: Store<any>): void {
        this.auth = authModule.context(store)
    }
}
@vdkrasny
Copy link
Contributor

You have to help to the TS compiler using Non-null assertion operator

import {auth as authModule} from './auth'

class GalleryActions extends Actions<GalleryState, GalleryGetters, GalleryMutations, GalleryActions> {

    auth!: Context<typeof authModule> // Just add "!" after the module name

    $init(store: Store<unknown>): void {
        this.auth = authModule.context(store)
    }
}

You can see this trick here: https://github.com/ktsn/vuex-smart-module#mocking-nested-modules-and-dependencies

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

No branches or pull requests

2 participants