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

Exception in TS if @observable has no initializer #1072

Closed
farwayer opened this issue Jul 3, 2017 · 2 comments · Fixed by #1081
Closed

Exception in TS if @observable has no initializer #1072

farwayer opened this issue Jul 3, 2017 · 2 comments · Fixed by #1081
Labels

Comments

@farwayer
Copy link
Contributor

farwayer commented Jul 3, 2017

If you try to observe (intercept, ...) property without initial value before first access then you will get exception. I think we need to add runLazyInitializers to TS also. Or even rewrite logic to make init more consistency between TS and JS. It's really bit hacky now.

import {observable, observe} from 'mobx'

class User {
  @observable
  loginCount;
}

const user = new User();
observe(user, 'loginCount', () => {});
tsc --experimentalDecorators test.ts
/mnt/data/dev/react/mobx-decorators/node_modules/mobx/lib/mobx.js:2211
        throw new Error("[mobx] Invariant failed: " + message + (thing ? " in '" + thing + "'" : ""));
        ^

Error: [mobx] Invariant failed: Cannot obtain atom from [object Object]
    at invariant (/mnt/data/dev/react/mobx-decorators/node_modules/mobx/lib/mobx.js:2211:15)
    at fail (/mnt/data/dev/react/mobx-decorators/node_modules/mobx/lib/mobx.js:2206:5)
    at getAtom (/mnt/data/dev/react/mobx-decorators/node_modules/mobx/lib/mobx.js:3129:12)
    at getAdministration (/mnt/data/dev/react/mobx-decorators/node_modules/mobx/lib/mobx.js:3134:34)
    at observeObservableProperty (/mnt/data/dev/react/mobx-decorators/node_modules/mobx/lib/mobx.js:3178:12)
    at Object.observe (/mnt/data/dev/react/mobx-decorators/node_modules/mobx/lib/mobx.js:3170:16)
    at Object.<anonymous> (/mnt/data/dev/react/mobx-decorators/test.js:19:8)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
@mweststrate
Copy link
Member

Can you set up a reproduction? Ideally a PR with an additional typescript unit test?

@mweststrate
Copy link
Member

Released fix as 3.1.17

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

Successfully merging a pull request may close this issue.

2 participants