-
Notifications
You must be signed in to change notification settings - Fork 7
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
Proposal: Add type checking to can-define & can-observe #334
Comments
Some other ideas:
I think it’s nice to have “type” in the name so it’s clear it corresponds with the type specified, vs. any of the other properties. |
@chasenlehara would these replace
I like this because it would work well with possible class MyType extends observe.Object {
@observe.checkType( [Number, null] )
age=null
} |
What would we do if both properties are the same then? If both are false, I guess it would be fine. If both are true, would you ignore the conversion and throw? |
That’s an interesting idea… running with that, maybe something My only concern would be if both Also for |
I think I would prefer just sticking with the current properties and adding some constructors that handle throwing if passed the wrong thing
|
@chasenlehara as I'm creating |
@phillipskevin Technically, this makes things a bit tricker (and harder for folks that want to create their own types). Currently, the Oh, there's another reason ... the error propagation should contain the name of the property .... isMember allows us to do something like this: set age(value){
if(!MaybeNumber.isMember(value) ){
throw prop+"value isn't MaybeNumber".
}
} We'd have to use a try/catch/throw if it was entirely w/i the set age(value){
try{
var converted = MaybeNumber.new(value)
} catch(e){
throw prop+"value isn't MaybeNumber".
}
...
} |
tldr; Lets create a type setting that enforces the type!
Problem
Related to #173, it would be nice for type settings to enforce the type. For example, the following specifies that
age
will be of typenumber
(or undefined or null):But if initialized or set to a non-number value, it will try to type-coerce the value:
This can sometimes be what you want, more often it's not:
Solution
I think it would be valuable to actually enforce type. Ideally, by throwing an error if the type was set to the wrong value:
As
type
andtypeCheck
would be asymmetrical, I think we should move the existing type conversion behavior to atypeConvert
property behavior:type
would still be backwards compatible, but deprecated in the docs.Alternate APIs
age: {checkType: MaybeNumber}
andage: {convertType: MaybeNumber}
age: {strictType: MaybeNumber}
age: {type: "number", convert: false}
age: {type: "number", strict: true}
Other Considerations
We support
Type: Foo
.I think
Type: Foo
can also be deprecated.can-reflect.isConstructorLike()
should be able to tell the difference between a constructor function and a normal function in all reasonable cases. In the cases where it can not, someone would just have to decorate it with thecan.new
symbol, to make a function that should be a constructor appear like a constructor.can-observe
I'd like something similar to work for can-observe eventually, decorating class-fields:
The text was updated successfully, but these errors were encountered: