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

How can I use the classic Omit functionality applied to an array type instead of a type in typescript?

How can I use the classic Omit functionality applied to an array type instead of a type?

For example, I’ve the following type

type Car = {
  a: number,
  b: string,
  c: Record<string, unknown> | null
}

type Cars = Car[]

I would like to create similar types without c: Record<string, unknown> | null.

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

For example I could declare.

type Voiture = Omit<Car, 'c'>

type Voitures = Omit<Cars, 'c'>  // obviously not working

For code constraints, I ca’t use Omit<Car, 'c'>[].

Is there a solution for this?

Thanks

>Solution :

You could use an Indexed Access Type for that to extract Car from Car[] then use Omit as usual and finally turn it back into an array.

type Car = {
  a: number,
  b: string,
  c: Record<string, unknown> | null
}

type Cars = Car[];

type Voitures = Omit<Cars[number], "c">[]
//   ^? type Voitures = Omit<Car, "c">[] 

TypeScript Playground

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