Throughout our application, we represent various data objects as classes.
These objects have a tree structure by nature, where parent objects are
instantiated with properties that are collections of complex data objects.
While we can refer to classes in typing within components, we lose the
benefit of excess property type checking that you would get with
interfaces. What is the best way to strictly type objects with nested data
objects, that also need to use instance methods to access or manipulate
properties of the object? Should we be using constructors with generic
types?
Current Implementation Example:
// DataModel is a class with id, type, name and title properties - we
extend from this class a lot.
export class Datasource extends DataModel {
features: Array<Feature>;
tables: Array<DatasourceTable>;
isSelected: boolean;
constructor(options) {
options.typ = 'datasource';
super(options);
this.features = (options.features) ? options.features : [];
this.tables = (options.tables) ? options.tables : [];
this.isSelected = (typeof options.isSelected !== 'undefined') ?
options.isSelected : false;
}
get selectedTable() {
/*
find and return the DatasourceTable that has an isSelected property
set to true.
*/
}
}
In the above instance, you could pass an object that missed some
properties, or had properties that should not have been included, and it
would not throw an error. You are also required to instantiate the array
properties of the class, setting default values either in the property
definition or in the constructor.
What is the best way to strictly type complex data objects without losing
the benefits that come with being able to construct instances of classes
that have public methods?
I'm not sure if we should be using generic types, or pairing classes with
interfaces, but there has to be a good approach for this.
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.