Considering adding type to my existing API, I have a function that can accept a string, function, or object (utilizing overloading). However, it is also able to accept an array of mixed values.
Is there a way to have an Array in TypeScript that consists of strings, functions, or plain objects only, and throws an error for any other types?
UPDATE: In response to feedback, I made the following changes:
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) {
}, {});
However, I encountered an error in the TypeScript playground related to the signature overload not matching the implementation and invocation.