-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Migrate from extendObservable to decorate? #1448
Comments
The problem is that you are running decorate with typescript strictness, but not building your class through the typescript standards. import { decorate, observable } from "mobx";
class Todo {
finished = false;
id = Math.random();
title = ""
}
decorate(Todo, {
finished: observable,
title: observable
}); If you don't have field initializers enabled in your config, use: class Todo {
finished; // declare these fields so TS knows about them
id;
title;
constructor() {
this.finished = false;
this.id = Math.random();
this.title = "";
}
}
decorate(Todo, {
finished: observable,
title: observable
}); |
App is JavaScript, can't declare anything on class. Works on desktop as a set of scripts, no build step at all. TS acts as a linter (allowJs, checkJs), and does know these fields are present on every instance, since type checking in strict mode and auto complete work fine throughout the app, only MobX upgrade broke. |
but your error is based on the typescript types, if the inferred type of Todo is not what you expect it would to be, there is not much we can do I think, probably there is a comment to make decorate ignore the typings or something. |
Inferred type of Todo is exactly what I expect it to be :) Let's look into MobX typings.
Looks like it wants a decorator for every property, while the Todo needs only export declare function decorate<T>(clazz: new (...args: any[]) => T, decorators: {
[P in keyof T]?: MethodDecorator | PropertyDecorator;
}): void; Yet another error, finally with enough detail:
Not sure how to alter typings from here. Where in MobX should I look? Update, the conflict seems to happen between observable.d.ts from MobX and lib.es5.d.ts from TypeScript itself: // observable.d.ts
export interface IObservableFactory {
(value: number | string | null | undefined | boolean): never;
// --> key: string <--
(target: Object, key: string, baseDescriptor?: PropertyDescriptor): any;
<T = any>(value: T[], options?: CreateObservableOptions): IObservableArray<T>;
<K = any, V = any>(value: Map<K, V>, options?: CreateObservableOptions): ObservableMap<K, V>;
<T extends Object>(value: T, decorators?: {
[K in keyof T]?: Function;
}, options?: CreateObservableOptions): T & IObservableObject;
}
// lib.es.5.d.ts
// --> propertyKey: string | symbol <--
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void; |
@makepost the types are defined here, and your Todo should take the first overload: Lines 3 to 10 in 057df95
|
Can you configure your project to use |
Should take the first overload, but doesn't because of the conflict I explained. The project has // Before:
(target: Object, key: string, baseDescriptor?: PropertyDescriptor): any;
// After:
(target: Object, key: string | symbol, baseDescriptor?: PropertyDescriptor): any; |
that change makes sense in general, although not sure why it makes a difference here. Feel free to submit a PR |
As I'm adding a case and enable type checking for test("decorate should work", function () {
class Box {
sizes = [2]
// ...
addSize() {
this.sizes.push([3])
}
}
}) I see a lot of this; the question is whether to add |
Following the example from readme:
However, the code is highlighted as an error:
What am I doing wrong?
Have to move from extendObservable, because it "can only be used to introduce new properties" now, so this crashes in development:
including stacktracebased on this sandboxtsconfig.json:
The text was updated successfully, but these errors were encountered: