下面的示例https://mobx.js.org/refguide/computed-decorator.html使用TypeScript引发错误。
// Parameterized computed views:
// Create computed's and store them in a cache
import { observable } from "mobx"
import { computedFn } from "mobx-utils"
class Todos {
@observable todos = []
getAllTodosByUser = computedFn(function getAllTodosByUser(userId) {
return this.todos.filter(todo => todo.user === userId))
})
}'this‘隐式有'any’类型,因为它没有类型annotation.ts(2683)
外部值“this”由此容器隐藏。
将tsconfig的noImplicitThis设置为false将解决此问题,但我的目的是将noImplicitThis设置为true。
有什么想法吗?谢谢!
发布于 2019-12-10 10:06:02
注射this: Todos可以解决这个问题。TypeScript不会抱怨,this将被正确地键入。注意,TS编译器在编译到JavaScript时将移除JavaScript参数。
// Parameterized computed views:
// Create computed's and store them in a cache
import { observable } from "mobx"
import { computedFn } from "mobx-utils"
class Todos {
@observable todos: ITodo[] = []
getAllTodosByUser = computedFn(function getAllTodosByUser(this: Todos, userId: ITodo[]) {
return this.todos.filter(todo => todo.user === userId))
})
}https://stackoverflow.com/questions/59264167
复制相似问题