pocesar
pocesar

Reputation: 7050

Mixed array of numbers and strings

How can I create an interface for an array that accepts both numbers and strings?

Since inside the function [1,'1'], ['1','1'],[1,1] are equivalent (they are joined inside as '1.1'), I can't seem to satisfy the compiler. It gets me TS2087: Could not select overload for 'call' expression.

works for fn([1,1]); and fn(['1','1']); but not mixed values.

Upvotes: 1

Views: 87

Answers (1)

Paleo
Paleo

Reputation: 23692

It's not possible. I suggest to use any[]:

function fn(arr: any[]) {
    alert(JSON.stringify(arr));
}
fn([1, 1]);
fn(['1', '1']);
fn(['1', 1]);

Upvotes: 2

Related Questions