1

Consider the following type:

type myType = {
  a: string,
  b: string
}

Let's say, that at one point of my script, a function could take a myType object, but can work with the a property as a number also. To make this new type for my function I could write:

type myNewType = Omit<myType, 'a'> & {
  a: string | number
}

Is there any shorter way to write this? I tried with simply extending the base object, like this:

type myNewType = myType & {
  a: number
}

but that results in a being identified as string & number instead of string | number.

Rahul Sharma
  • 5,562
  • 4
  • 24
  • 48
Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • 1
    From this https://stackoverflow.com/questions/41285211/overriding-interface-property-type-defined-in-typescript-d-ts-file it appears that using Omit is still the conventional wisdom, but others have built utility types on top of the Omit approach which is perhaps semantically cleaner https://stackoverflow.com/a/55032655/9987590 – nate-kumar Oct 16 '22 at 09:37

0 Answers0