Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Cannot override property when extending structs #314

Open
rkmax opened this issue Mar 9, 2018 · 5 comments
Open

Cannot override property when extending structs #314

rkmax opened this issue Mar 9, 2018 · 5 comments

Comments

@rkmax
Copy link
Contributor

rkmax commented Mar 9, 2018

Version

3.2.25

Expected behaviour

When extend a struct be able to overwrite props

Actual behaviour

Throws an error

Invalid call to mixin(target, source, [overwrite]): cannot overwrite property "X" of target object

Steps to reproduce

const t = require('tcomb');
const A = t.struct({a: t.String});
const B = t.struct({b: t.String});
const MyType = t.struct({
  myList: t.list(A)
});

const MyExtend =  MyType.extend({
  myList: t.list(B)
});

Stack trace and console log

/home/rkmax/my_project/node_modules/tcomb/lib/fail.js:2
  throw new TypeError('[tcomb] ' + message);
  ^

TypeError: [tcomb] Invalid call to mixin(target, source, [overwrite]): cannot overwrite property "worksheets" of target object
    at Function.fail (/home/rkmax/my_project/node_modules/tcomb/lib/fail.js:2:9)
    at assert (/home/rkmax/my_project/node_modules/tcomb/lib/assert.js:14:12)
    at mixin (/home/rkmax/my_project/node_modules/tcomb/lib/mixin.js:11:11)
    at /home/rkmax/my_project/node_modules/tcomb/lib/extend.js:50:5
    at Array.forEach (<anonymous>)
    at extend (/home/rkmax/my_project/node_modules/tcomb/lib/extend.js:43:10)
    at extendStruct (/home/rkmax/my_project/node_modules/tcomb/lib/struct.js:19:10)
    at Function.Struct.extend (/home/rkmax/my_project/node_modules/tcomb/lib/struct.js:112:12)
    at Object.<anonymous> (/home/rkmax/my_project/src/types/worksheet.js:189:46)
    at Module._compile (module.js:643:30)

@gcanti
Copy link
Owner

gcanti commented Mar 10, 2018

@rkmax actually this is intentional

@ArmorDarks
Copy link

Sounds like a special option like override which will allow to override them will be a good addition.

@rkmax
Copy link
Contributor Author

rkmax commented Mar 10, 2018

then I'm misunderstanding the use of struct.extend functionality I was thinking in something like the extend of the classes where you can add or override things

@rodoabad
Copy link

@rkmax I wouldn't call that just purely "extending" anymore if you're overriding an existing property already.

@ArmorDarks
Copy link

@rodoabad why is that? In the wild extending does not imply that overriding isn't possible. Refer to the Classes, where overriding is a crucial part of the extending.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants