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

observable.ref and decorate() function #1713

Closed
davidmz opened this issue Sep 3, 2018 · 5 comments
Closed

observable.ref and decorate() function #1713

davidmz opened this issue Sep 3, 2018 · 5 comments

Comments

@davidmz
Copy link

davidmz commented Sep 3, 2018

I am trying to use MobX (5.1) with TypeScript (3.0) but without a decorators syntax. Docs recommends to replace @-directives by decorate(Class, {}) function. There is sample code:

import { decorate, observable } from 'mobx';

class A {
  foo?: object;
}

decorate(A, {
  foo: observable.ref,
});

And this code produces the following TS error in my environment:

Argument of type '{ foo: IObservableDecorator; }' is not assignable to parameter of type '{ prototype?: MethodDecorator | PropertyDecorator | MethodDecorator[] | PropertyDecorator[] | undefined; }'.
  Object literal may only specify known properties, and 'foo' does not exist in type '{ prototype?: MethodDecorator | PropertyDecorator | MethodDecorator[] | PropertyDecorator[] | undefined; }'.

There is obviously something wrong with types but in fact this code works: https://codesandbox.io/s/znyp2x33xx

What am I doing wrong and what is the recommended way to do such things?

@mweststrate
Copy link
Member

mweststrate commented Sep 3, 2018 via email

@davidmz
Copy link
Author

davidmz commented Sep 4, 2018

No, it does not.

I found that the problem is related to the strictFunctionTypes flag in tsconfig.json. This flag is true in my environment, if I set it to false, the error disappears. I made a small test repository to demonstrate this issue: https://github.com/davidmz/mobx-decorate-test.

@davidmz
Copy link
Author

davidmz commented Sep 4, 2018

Hmm, strictFunctionTypes isn't always working… Looks like TS chooses invalid definition for 'decorate', decorate<T>(object: T, … instead of decorate<T>(clazz: new…, and requires keys of class function as object (i. e. prototype).

@nykula
Copy link
Contributor

nykula commented Sep 7, 2018

Please review pull requests #1711 and #1712, they fix calling decorate with modifiers. This is connected to an older issue, #1448.

@mweststrate
Copy link
Member

This changes has been released yesterday, closing the issue

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

3 participants