0

The below code:

class A {constructor(v) {
    this.v = v;
}}
const a = fromJS(new A({a: 42, someOtherSubTree: {}}));
const aMod = a.updateIn(['v', 'a'], x=>43);

… fails with:

TypeError: a.updateIn is not a function

The simpler set also fails with:

TypeError: a.set is not a function

I've found this related question which is however tagged with 'typescript' so I am not sure how the "wrapping" approach suggested there would translate to pure ES6.

Community
  • 1
  • 1
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
  • Are you sure you need classes? If you need strict structure and "methods", you can use Immutable.js `Record`s https://facebook.github.io/immutable-js/docs/#/Record/Record ... here is nice example https://gist.github.com/prenaudin/f1cad2bda2f2334459f8b006449c91e4#file-1-models-task-js – Pavel 'Strajk' Dolecek Dec 14 '16 at 10:59

1 Answers1

1

After const a = fromJS(new A({a: 42, someOtherSubTree: {}})); the typeof a is still an object and to Map like expected. It returns the original object.

This part from the Immutable code shows why. Because your object A is not a "plain object" (see function) nor an array, it's just returned as is.

function fromJSDefault(json) {
    if (Array.isArray(json)) {
      return IndexedSeq(json).map(fromJSDefault).toList();
    }
    if (isPlainObj(json)) {
      return KeyedSeq(json).map(fromJSDefault).toMap();
    }
    return json;
}
// […]
function isPlainObj(value) {
    return value && (value.constructor === Object || value.constructor === undefined);
}
michelgotta
  • 976
  • 8
  • 11