0

Let's say I have an interface:

interface FooBar {
  foo: string
  bar: {
    baz: string
  }
}

I'd like to construct a type based FooBar's bar property, with only its properties:

interface Bar {
  baz: string
}

Trying Pick<Foobar, 'bar'> only yields:

{
  bar: {
    baz: string
  }
}
2
  • 1
    Does this do what you want? type Bar = FooBar["bar"] Commented Mar 7, 2020 at 2:30
  • @NicholasTower Yes! Would you like to put this as the answer? Commented Mar 7, 2020 at 2:34

1 Answer 1

1

You can refer to a property inside a type with square brackets:

type Bar = FooBar["bar"];

const example: Bar = {
   baz: 'hi'
};
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.