When observe() a MST node's primitive property, TypeScript get the wrong type of oldValue & newValue
songzhenqi opened this issue · comments
song zhen qi(宋镇淇) commented
Bug report
- I've checked documentation and searched for existing issues and discussions
- I've made sure my project is based on the latest MST version
- Fork this code sandbox or another minimal reproduction.
Sandbox link or minimal reproduction code
import { observe } from "mobx"
import { types } from "mobx-state-tree"
const Model = types
.model('Model', {
name: types.string,
})
.actions((self) => ({
setName(newName: string) {
self.name = newName
},
}))
const model = Model.create({
name: "John",
})
observe(model, 'name', (change) => {
if (change.type === 'update') {
console.log(`observe storedValue, ${change.oldValue?.storedValue}, ${change.newValue?.storedValue}`)
console.log(change.oldValue)
console.log(`observe, ${change.oldValue}, ${change.newValue}`)
}
})
model.setName("Dave");
Describe the expected behavior
Typescript inferring the oldValue & newValue as ScalarNode
Describe the observed behavior
TypeScript inferring the oldValue & newValue as string
Subramanya Chakravarthy commented
The store value is of type any
because of references.
This is from the Code
// usually the same type as the value, but not always (such as with references)
Maybe we need to cast the Model type if it's not a reference