0

After reviewing the documentation and this SO Question it looks like the behavior I am looking for is the following:

type A = {
  a:string
};
type B = {
  b:string
};

type C = A & B & {
   c:string
};

My question is: Can the same behavior be done with interfaces in a single line?

4
  • I vaguely recall seeing a duplicate out there on this question, but I can't find it. Commented Jan 26, 2021 at 16:59
  • 1
    Please consider giving a minimal reproducible example of what you're trying to do, with enough structure to distinguish good answers from bad answers. Right now it's a bit too minimal: you've got empty object types, so Primary1 and Primary2 are equivalent to each other and Secondary is equivalent to just {prop: string}. You can extend multiple interfaces like interface Secondary extends Primary1, Primary2 { prop: string }, but I'm not sure if that actually meets whatever use case you have without a little more in the example. Commented Jan 26, 2021 at 17:02
  • I made a guess, hopefully that's what you're looking for Commented Jan 26, 2021 at 17:07
  • Thanks, I updated it. Commented Jan 26, 2021 at 18:06

1 Answer 1

4

You can extend a comma-separated list of interfaces at once, as long as the interfaces don't have overlapping properties of differing types:

interface Primary1 { prim1: string };
interface Primary2 { prim2: string };
interface Secondary extends Primary1, Primary2 { prop: string }

declare const secondary: Secondary;
secondary.prim1.toUpperCase(); // ok
secondary.prim2.toUpperCase(); // ok
secondary.prop.toUpperCase(); // ok

Playground link to code

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.