0

Does Typescript have the concept of subset interfaces.

There is a parent interface parent which looks like:

interface parent {
    a?: string;
    b?: string;
}

There are other interfaces one and two.

interface one {
    a: string
}
interface two {
    b: string
}

Both one and two shouldn't be able to add fields that aren't present in the parent interface. is this possible?

Marcus
  • 9,032
  • 11
  • 45
  • 84

1 Answers1

1

Both one and two shouldn't be able to add fields that aren't present in the parent interface.

This is somewhat counter to TypeScript's structural typing philosophy, but see NoExtraProperties at https://gist.github.com/greglockwood/1610ef83d0726e0e6c021d46cb573e68 if you really need this functionality.

The gist was sourced from Is it possible to restrict typescript object to contain only properties defined by its class ? which might also be helpful for additional context.

Casey Woolfolk
  • 611
  • 5
  • 5