Problem:
Have the following function:
function x(p: string | string[]): string | string[];
How do I ensure that the following just outputs a single string value?
console.log(x());
I.e.:
console.log(x("Hello")); // should be "Hello"
console.log(x(["Hello", "World"])); // should be "Hello"
Any shortcut in JavaScript?
Solution:
like this:
function x(p:string|string[]){
return Array.isArray(p)?p[0]:p
}