I need to do a pretty simple task as crawl input arguments and stringify all Big Numbers (BN's) if there are any. I want to specify proper types but can't find a way to do that so currently my code looks like that
function stringifyBN(value: unknown): unknown {
if (isBN(value)) {
return value.toString();
}
if (Array.isArray(value)) {
return value.map((v) => stringifyBN(v));
}
return value;
}
unknown basically does nothing regarding guarding from errors.
I tried the approach below since my input is either a primitive value or a recursive array of primitive values and it's always an array on the top level.
function stringifyBN(
value: number | string | boolean | BN | number[] | string[] | boolean[] | BN[]
): number | string | boolean | number[] | string[] | boolean[] {
if (isBN(value)) {
return value.toString();
}
if (Array.isArray(value)) {
return value.map((v) => stringifyBN(v));
}
return value;
}
but got an error
Type '(string | number | boolean | string[] | number[] | boolean[])[]' is not assignable to type 'string | number | boolean | string[] | number[] | boolean[]'.
Type '(string | number | boolean | string[] | number[] | boolean[])[]' is not assignable to type 'string[]'.
Type 'string | number | boolean | string[] | number[] | boolean[]' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'. TS2322
Any other approaches don't work neither since I get similar errors and already ran out of ideas. TS should basically help writing code instead creating troubles but I have the second option here.
Could someone help with that?
Thank you in advance!