Skip to content

Throw/warn/snapshot-itize instances passed as argument to types.optional #1002

@xaviergonz

Description

@xaviergonz

I have:

  • A conceptual question.
    • I've checked documentation and searched for existing issues
    • I tried the spectrum channel first
  • I think something is not working as it should.
    • I've checked documentation and searched for existing issues
    • I've made sure your project is based on the latest MST version
    • Fork this code sandbox or another minimal reproduction.
    • Describe expected behavior
    • Describe observed behavior
  • Feature request
    • Describe the feature and why you feel your feature needs to be generically solved inside MST
    • Are you willing to (attempt) a PR?

Not following the above template might result in your issue being closed without further notice

Today a coworker was telling me that it took them a while to find a bug related to passing an instance as argument to types.optional, since as soon as a second node of the same type was created it would crash because it was attached to the first node parent.

const someDefault = TModel1.create({...})

const TModel2 = types.create({ m1: types.optional(TModel1, someDefault) })
const a = TModel2.create({}) // ok
const b = TModel2.create({}) // boom!

My question is, should that particular case be prevented?
If so, what would the best course of action?

  • A. snapshot-ize the instance - mostly non-breaking, but might loose the environment (not a big deal, since anyway children and parents have to have the same environment anyway)
  • B. throw an error - breaking-ish change though?
  • C. (my least favorite) just show a console warning

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions