-
-
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
Typescript + Mobx observable([]).remove(...) #669
Comments
Can you show a full example I can't reproduce. |
This looks weird, since But if you're using it as a decorator ( |
Closing for the lack of a reproduction |
@mweststrate class Todo {
store: TodoStore
value: string
constructor(store: TodoStore, value: string) {
this.store = store
this.value = value
}
@action destroy() {
this.store.remove(this)
}
}
class TodoStore {
title: string = 'TEST'
@observable todos: Todo[]
constructor(title: string) {
this.title = title
this.todos = [new Todo(this, 'test')]
}
@action removeTodo(todo: Todo) {
this.todos.remove(todo) // Property 'remove' does not exist on type 'Todo[]'.
}
} but if we set todos to IObservableArray everything works class TodoStore {
title: string = 'TEST'
@observable todos: IObservableArray<Todo>
constructor(title: string) {
this.title = title
this.todos = [new Todo(this, 'test')] as IObservableArray<Todo>
}
@action removeTodo(todo: Todo) {
this.todos.remove(todo)
}
} Is it ok to write like this? |
Yup that's how you should do it. But I'd rather do something like this instead: class TodoStore {
title: string = 'TEST'
readonly todos = observable<Todo>([])
constructor(title: string) {
this.title = title
this.todos.push(new Todo(this, 'test'));
}
@action removeTodo(todo: Todo) {
this.todos.remove(todo)
}
} This way you have no array annotation and you prevent any array reassignment by marking it readonly (you don't need an |
@JabX it makes sense. |
This goes against the point of the @observable annotation... |
@ehartford yes there is since Mobx 3, use
|
An annotation cannot change the type of the property (or class, or method) it is applied to, and I believe it to be the case for all the languages I know of. |
@JabX The annotation does, in fact, at runtime, change the type from a native array into an ObservableArray. It's just a question of whether TypeScript is aware of the change and able to provide correct dev-time and compile-time validation. If TypeScript doesn't give us the tools to describe the change, then it's a TypeScript bug. If we can describe it but we aren't, it's bug in the MobX typings. |
@ehartford I was talking about compile-time type information, sorry for the inaccuracy. Sure, it does indeed change the type at runtime, which I consider to be a bad practice in general because of the impossibility of describing it at compile-time. Just don't use the annotation if need the type info, @mweststrate's solution (which is also what I proposed a few posts earlier) is better anyway. |
I cant follow this, I'm getting the same error as given above, and I'm simply observing an array of strings:
This is just a number of typescript and MObX issues I'm having, when trying to stick to the docs. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions. |
I am getting a
with mobx@2.6.2, webpack with awesome-typescript-loader@2.2.4 and typescript@2.0.8.
The text was updated successfully, but these errors were encountered: