JsonApiModel mapped typing
Hey I think the JsonApiModel type isn't entirely correct, or very possibly I'm just not understanding it.
It has this in it's type / class declaration: [key: string]: any;, but this causes the models to accept any properties, even ones not listed on the model, for eg:
export class User extends JsonApiModel{
@Attribute()
name: string;
}
const user = datastore.createRecord(User);
user.name = 'someName'; // this is correct and should work
user.anything = true; // this IMO should throw an error in TS but doesn't because of [key: string]: any;
So my question is, should this be changed? Or does it serve a purpose I'm just not understanding?
I don't know if this serves a purpose and without this line the tests are still running through.
But I think this would be a breaking change, at least for the way I am using this library at some places:
I use the models directly in my view. To render my models I often need some additional information that will be calculated by the client, e.g. a locked or hidden flag when rendering my models in a list. For convenience I just set them as property in my models but (lazy as I am) do not declare them in the model class, because this is nothing that is coming from the server.
I know there are probably more elegant ways to solve this (although it could be tricky as our models aren't POJOs) and an easy solution would be to just add the properties in the model class. But in the current state this change would break my application (and maybe those of others).
So if we change it, it should be changed in the next major version.
I don't think that would affect your use case though? You normally extend from the base class, so if you add any properties (be it methods or getter or whatever) they should work normally right?
Or do you add those "decorator-like-thingies" directly to a record, not to the class itself?
edit: aaa.. I think I get what you mean, something like:
- Fetch User data
- Add some properties to the user records (hidden/locked/etc)
- Use those properties in the records
I was originally thinking you mean something like:
export class User extends JsonApiModel {
(... attributes here)
get locked() { ... }
}
So yeah it would be an easy way to solve this breaking issue (you would need to subclass JsonApiModel and use that), but it would be a breaking change in that case.