use Partial in nested property with typescript

前端 未结 4 979
慢半拍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:23

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

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

    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

提交回复
热议问题