Make all properties within a Typescript interface optional

后端 未结 5 601
离开以前
离开以前 2020-12-12 18:48

I have an interface in my application:

interface Asset {
  id: string;
  internal_id: string;
  usage: number;
}

that is part of a post int

5条回答
  •  盖世英雄少女心
    2020-12-12 19:08

    In addition to David Sherret answer just a few lines from my side how it can be implemented directly without Partial type for better understanding of the subject.

    interface IAsset {
      id: string;
      internal_id: string;
      usage: number;
    }
    
    interface IPost {
      asset: IAsset;
    }
    
    interface IPostDraft {
      asset: { [K in keyof IAsset]?: IAsset[K] };
    }
    
    const postDraft: IPostDraft = {
      asset: {
        usage: 123
      }
    };
    

提交回复
热议问题