2

I'm trying to build a type to describe a "simple object", which is an object which contains only primitive types, other objects and arrays which also only contain primitive types... or other objects and arrays, etc etc

type Primitive = string | number | boolean
type SimpleArray = Array<Primitive | SimpleObject | SimpleArray>
type SimpleObject = Record<string, Primitive | SimpleObject | SimpleArray>

However I am being told that I cannot use the SimpleObjecttype declaration recursively. Is there a way to describe this type?

1 Answer 1

2

You can fix this by replacing Record<string, ...> with { [key: string]: ... } (which is basically the same):

type SimpleObject = { [k: string]:  Primitive | SimpleObject | SimpleArray }

By the way, you can simplify all the above to:

type SimpleObject = string | number | boolean | SimpleObject[] | { [key: string]: SimpleObject };
Sign up to request clarification or add additional context in comments.

Comments

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.