We use a simple declaration of a function quite a lot, where the function accepts either a single object or an array of objects of a certain type.
Simple announcement:
interface ISomeInterface { name: string; } class SomeClass { public names: ISomeInterface[] = []; public addNames(names: ISomeInterface | ISomeInterface[]): void { names = (!Array.isArray(names)) ? [names] : names; this.names = this.names.concat(names); } }
But TypeScript throws "type not assignable" error.
Is there a better way to do this? Obviously, we can have two separate functions, but I think that managing a single vs-plural way is pretty good.
source share