Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

options to extend an interface with one different value without having to re-write that object?

What is the best approach to make a similar interface that is different in only one value, then to duplicate code and rewrite the object again ? Could this be done without union type ?

// A1
export interface FirstType {
  id: string;
  parentId: string;
  type: 'a1';
  data : string[]
  dataA : string[]
  dataB : string[]
  dataC : string[]
  dataD : string[]
  dataE : string[]
}

// A2
export interface SecondType extends FirstType {
   type: 'a2';
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use the Omit<T, K> utility type to produce a named type suitable for interface extension:

export interface SecondType extends Omit<FirstType, "type"> {
    type: 'a2';
}

Playground link to code

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading