0

We have an interface defined this way:

interface IInitialInterface {
   stack: string,
   overflow: {
      a: string,
      b: string
   }[],
}

How can I create a type with only the overflow property removing the array? If I do:

type TypeSearched = IInitialInterface['overflow']

Then I get:

type TypeSearched = {
    a: string,
    b: string
}[]

But I want to remove the array.

Thank you.

2 Answers 2

2

To get the member type of an array you can drill in with ArrayType[number]. This returns the type that would be returned if you accessed that array with any number.

type TypeSearched = IInitialInterface['overflow'][number]
// { a: string; b: string; }

However, a cleaner approach would be to build the pieces from named little pieces, rather than disassembling the big piece.

interface TypeSearched {
   a: string,
   b: string
}

interface IInitialInterface {
   stack: string,
   overflow: TypeSearched[]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Alex. I was asking for the first method but I think the second one is much cleaner code. Very instructive. Thanks.
2

overflow property is not "typed" in this case, you can do this

interface IInitialInterface {
   stack: string,
   overflow: IOverflow[],
}

interface IOverflow {
   a: string,
   b: string
}

Then, you can refer to IOverflow of directly instead of through IInitialInterface

type TypeSearched: IOverflow; 

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.