diff --git a/src/define.js b/src/define.js index aeb1a47..40af769 100644 --- a/src/define.js +++ b/src/define.js @@ -24,7 +24,8 @@ var newSymbol = Symbol.for("can.new"), inSetupSymbol = Symbol.for("can.initializing"), isMemberSymbol = Symbol.for("can.isMember"), hasBeenDefinedSymbol = Symbol.for("can.hasBeenDefined"), - canMetaSymbol = Symbol.for("can.meta"); + canMetaSymbol = Symbol.for("can.meta"), + baseTypeSymbol = Symbol.for("can.baseType"); var eventsProto, define, make, makeDefinition, getDefinitionsAndMethods, getDefinitionOrMethod; @@ -688,6 +689,18 @@ make = { return setter; } else { return function setter(newValue){ + //!steal-remove-start + if(process.env.NODE_ENV !== 'production') { + try { + return set.call(this, canReflect.convert(newValue, type)); + } catch (error) { + var typeName = canReflect.getName(type[baseTypeSymbol]); + var message = newValue + ' is not of type ' + typeName + '. Property ' + prop + ' is using "type: ' + typeName + '". '; + message += 'Use "' + prop + ': type.convert(' + typeName + ')" to automatically convert values to ' + typeName + 's when setting the "' + prop + '" property.'; + throw new Error(message); + } + } + //!steal-remove-end return set.call(this, canReflect.convert(newValue, type)); }; } diff --git a/test/define-test.js b/test/define-test.js index 9297840..741f0b4 100644 --- a/test/define-test.js +++ b/test/define-test.js @@ -2,6 +2,8 @@ const QUnit = require("steal-qunit"); const { mixinObject } = require("./helpers"); const { hooks } = require("../src/define"); const canReflect = require("can-reflect"); +const type = require('can-type'); +const dev = require("can-test-helpers").dev; QUnit.module("can-observable-mixin - define()"); @@ -188,3 +190,21 @@ QUnit.test("properties using value behavior reset when unbound", function(assert assert.equal(obj.derivedProp, undefined, "value reset"); }); + +dev.devOnlyTest('On error include the name of the property that is being set', function(assert) { + class Person extends mixinObject() { + static get props() { + return { + age: type.check(Number) + }; + } + } + + var farah = new Person(); + + try { + farah.age = '4'; + } catch (error) { + assert.equal(error.message, '4 is not of type Number. Property age is using "type: Number". Use "age: type.convert(Number)" to automatically convert values to Numbers when setting the "age" property.'); + } +});