skip to Main Content

If I have a type:

type MyType = {
    ...
    propertyA: string;
    propertyB: number;
    ...
}

how can I type this in another type but allow all versions of it with properties omitted? Such as:

type MyOtherType = {
    propertyC: MyType | Omit<MyType, ANYTHING>;
}

I have tried Omit<MyType, any> but that removes the types of other properties on the object.

What is the correct way to do this?

2

Answers


  1. I believe you means make all of them optional (Partial), you can try to use Partial<T> instead:

    type MyType = {
        ...
        propertyA: string;
        propertyB: number;
        ...
    }
    
    type MyOtherType = {
        propertyC: MyType | Partial<MyType>;
    }
    
    Login or Signup to reply.
  2. If i understood correctly. You might use the Partial utility type to make all props of MyType optional and then use Omit to remove any property you want from it.

    type MyOtherType = {
        propertyC: MyType | Omit<Partial<MyType>, 'propertyA'>;
    }
    

    It is an example. So I removed propertyA from MyType. You can remove other property with replacing ‘propertyA’ with the name of that property.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search