Encountering a TypeScript error due to this pattern:
Error message: 'Argument of type '(string | number)[]' is not assignable to parameter of type 'string[] | number[]'
function foo(value: string | number) {
return bar([value]); // <- TypeScript error
}
function bar(valueList: string[] | number[]) {
..does something...
}
It seems like the issue arises from mixing strings and numbers in an array.
Is there a more type-safe approach to solve this problem? Currently considering casting to any[]
, but unsure if it's the best course of action:
function foo(value: string | number) {
const valueList: any[] = [value];
return bar(valueList);
}