use Partial in nested property with typescript

前端 未结 4 972
慢半拍i
慢半拍i 2020-12-13 13:13

Say I have a type like this;

interface State {
  one: string,
  two: {
    three: {
      four: string
    },
    five: string
  }
}

I make

相关标签:
4条回答
  • 2020-12-13 13:21

    Try to exclude the nested Property, and add it again as Partial:

    interface Foo {
        someName: Partial<Omit<MainType, 'lvl2Partial'> & { lvl2Partial: Partial<SubType> }>
    }
    
    0 讨论(0)
  • 2020-12-13 13:23

    For TypeScript 2.8 or later, the following type should fix problem about Array property.

    type NestedPartial<T> = {
        [K in keyof T]?: T[K] extends Array<infer R> ? Array<NestedPartial<R>> : NestedPartial<T[K]>
    };
    

    Please see the example below.

    interface Foo {
        NumProp: number;
        SubItem: Foo;
        SubItemArray: Foo[];
    }
    

    Valid Result with conditional type

    Invalid Result

    https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html

    0 讨论(0)
  • 2020-12-13 13:27

    This is possible, you can create a 'deep' partial type as followed:

    type DeepPartial<T> = {
        [P in keyof T]?: DeepPartial<T[P]>;
    };
    

    Which can be used as followed

    const state: DeepPartial<State> = {
        two: {
            three: {
                four: '4'
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 13:40

    You can pretty easily define your own RecursivePartial type, which will make all properties, included nested ones, optional:

    type RecursivePartial<T> = {
        [P in keyof T]?: RecursivePartial<T[P]>;
    };
    

    If you only want some of your properties to be partial, then you can use this with an intersection and Pick:

    type PartialExcept<T, K extends keyof T> = RecursivePartial<T> & Pick<T, K>;
    

    That would make everything optional except for the keys specified in the K parameter.

    0 讨论(0)
提交回复
热议问题