Mobx Computed with Arguments (computedFn) 和 TypeScript - 这是什么?
Mobx Computed with Arguments (computedFn) and TypeScript - what's this?
以下示例 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' implicitly has type 'any' because it does not have a type annotation.ts(2683)
An outer value of 'this' is shadowed by this container.
将 tsconfig 的 noImplicitThis
设置为 false 将解决此问题,但我的意图是将 noImplicitThis
设置为 true
。
有什么想法吗?谢谢!
注入 this: Todos
将修复它。 TypeScript 不会报错并且 this
会被正确输入。注意TS编译器在编译成JavaScript.
时会去掉this
参数
// 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://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' implicitly has type 'any' because it does not have a type annotation.ts(2683)
An outer value of 'this' is shadowed by this container.
将 tsconfig 的 noImplicitThis
设置为 false 将解决此问题,但我的意图是将 noImplicitThis
设置为 true
。
有什么想法吗?谢谢!
注入 this: Todos
将修复它。 TypeScript 不会报错并且 this
会被正确输入。注意TS编译器在编译成JavaScript.
this
参数
// 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))
})
}