I need the create function to return a single model or an array of models based on the count value.
export default abstract class Factory<Model> {
protected _count: number = 1
private count(count: number): this {
this._count = count
return this
}
public create(): Model | Model[] {
return this.generate()
}
}
but the problem is that the client code needs to check if the result is an array or not which is not desired.
// data is Model|Model[] but should be Model[]
const date = factory.count(2).create()
// data is Model|Model[] but should be Model
const date = factory.create()
How can I set a condition for create's return type to be an array if the count > 1?