Is there a way to define a type/interface that can handle both objects and arrays without having to explicitly check each time?
I want to avoid using the `|` operator in TypeScript which requires checking if a variable is an array or not, as I always know if it's an object or an array. Instead of using `as SomeType` or `as SomeType[]`, I'd like to be able to use `?.` everywhere.
One solution I've tried is defining a mixture of object and array:
interface _Array<T> extends Array<T | undefined> {}
type ArrayORObject<T> = Partial<T> & Partial<_Array<T>>;
This almost works, but now I'm running into an issue where I get "Index signature for type 'number' is missing in type SomeObject." It seems that objects need an array index signature ([index: number]), but I can't figure out how to make it optional.
Edit: Upgrading to the beta version of TS isn't feasible right now, so adding calls to functions like makeOrder() isn't much better than just manually checking for arrays.
For a better example, you can check out this TS Playground