I have existing API that I thinking to add type to, and I have function that accept string function or object (I can use overloading for this) but it also accept array of mixed values like these.
Is it possible to have Array of strings, functions or plain objects in TypeScript? It should throw error for array with other types.
EDIT based on comments I added this:
function Terminal(interpreter: (string, object) => void, settings: object);
function Terminal(interpreter: (string | ((string, object) => void) | object)[], settings: object) {
if (typeof interpreter == 'function') {
interpreter("foo", {});
} else if (interpreter instanceof Array) {
interpreter.forEach((arg) => console.log(arg));
}
}
Terminal(["foo", function (command, term) { }], {});
Terminal(function(command) {
}, {});
but got error in TypeScript playground about overload signature don't match implementation and one from invocation.
(string | Function | object)[]string[] | Function[] | object[]