打字稿:ReferenceError:初始化前无法访问“存储”

zis*_*she 4 oop inheritance typescript

我有一个存储状态(使用mobx)的类存储。

export class Store<State> {
    @observable
    public state: State;

    constructor(protected rootStore: RootStore, state: State) {
        this.state = state || ({} as State);
    }

    @action
    setState(state: State) {
        this.state = {
            ...this.state,
            ...state
        };
    }
}

Run Code Online (Sandbox Code Playgroud)

我正在尝试实现一个类UserState

interface UserState {
    authorised?: boolean;
    loading?: boolean;
    name?: string;
    balance?: number;
}

export class UserStore extends Store<UserState> {
    constructor(rootStore: RootStore) {
        super(rootStore, {
            authorised: false,
            loading: true,
            name: ''
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

一切对我来说似乎都是正确的,但我有一个错误:

ReferenceError: Cannot access 'Store' before initialization

我只是尝试在存储中设置一些默认值,并且似乎在Store其内部的构造函数中,因此显然已对其进行了初始化。

在此处输入图片说明

zis*_*she 6

通过将Store类移到隔离文件(此文件与全局存储位于同一文件中)之前,解决了该问题。