Skip to content
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

When observe() a MST node's primitive property, TypeScript get the wrong type of oldValue & newValue #2105

Open
3 tasks done
songzhenqi opened this issue Oct 16, 2023 · 1 comment
Labels
bug Confirmed bug help/PR welcome Help/Pull request from contributors to fix the issue is welcome

Comments

@songzhenqi
Copy link

songzhenqi commented Oct 16, 2023

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

image

@chakrihacker
Copy link
Collaborator

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

@chakrihacker chakrihacker added bug Confirmed bug help/PR welcome Help/Pull request from contributors to fix the issue is welcome labels Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug help/PR welcome Help/Pull request from contributors to fix the issue is welcome
Projects
None yet
Development

No branches or pull requests

2 participants