2

How to set the type for an array like

["string", [”string", ["other string"],["any"]], [”string", ["string”, ["something string"]]], ....]

That is, the depth can be any. Number of arrays after index '0' is also. The main condition, the type under the index '0' must be 'string'.

1 Answer 1

1

From Ulad Kasach at https://stackoverflow.com/a/60722301/11745228,

This works:

type ValueOrArray<T> = T | ValueOrArray<T>[];
type NestedStringArray = ValueOrArray<string>;

Or, more directly for your answer:

type StringOrArray = string | StringOrArray[];
type NestedArray = StringOrArray;

Then just type your array with NestedStringArray

Sign up to request clarification or add additional context in comments.

1 Comment

I found almost the same thing on your link.type NestedArray = string | [ string, ...NestedArray[] ]; its work.thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.