0

given the following type:

type Foo = {
  a: {
    b: number
    c: string
  }
  d: number
}

is there a way to retrieve the anonymous nested types? I was thinking something like TypeOf<Foo, 'a'> should return the type made of b and c and TypeOf<Foo, 'd'> should be an alias to number. I made up the syntax, but I wanted to know if a similar concept exists

0

1 Answer 1

1

turns out you can do this by doing Foo['a']. Great!

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

3 Comments

If you're going to reuse the inner type, you should make a real type for clarity
that's not always possible. In my case this type comes from a code generator, so I am stuck with that. I still don't know how to handle complex cases like if a is optional, then Foo['a']['b'] will throw, do you know how to address this?
No, I had not even heard of this solution you found

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.