How can I ensure my Javascript array is of a specific type?
For instance, when creating a class like the one below:
export default class AnalyticData {
constructor(collection = []) {
return collection.map((item) => new AnalyticDatum(item))
}
}
After instantiating myData = new AnalyticData()
, the type of myData ends up being Array instead of AnalyticData
.
Is there a way to guarantee that my data is of type AnalyticData
rather than an Array?
I am facing this issue in my Vue component, where I have defined the following:
props: {
analyticData: {
type: AnalyticData,
required: true,
},
},
This results in the warning message:
[Vue warn]: Invalid prop: type check failed for prop "analyticData". Expected AnalyticData, got Array
Does anyone have a solution to create a typed Array in JavaScript?