简体   繁体   中英

Write interface of type object in typescript interface

This is my Data Structure how do I write an interface for it?

const TransitReport: {
        title: string;
        client: string;
        data: {
            overdueReviews: number;
            outstandingCovenantBreaches: number;
            outstandingMarginingBreaches: number;
            periodStartDate: string;
            periodEndDate: string;
        };
    }[]

I tried this:

export interface TransitReport {
  title: string;
  client?: string;
  data: Record<string, unknown>;
  overdueReviews: number;
  outstandingCovenantBreaches: number;
  outstandingMarginingBreaches: number;
  periodStartDate: string;
  periodEndDate: string;
}

This kinda works for mock API calling for data but when writing test cases I stuck with the error of:

Type '

{ title: string; 
client: string; 
data: { 
overdueReviews: number; 
outstandingCovenantBreaches: number; 
outstandingMarginingBreaches: number; 
periodStartDate: string;
periodEndDate: string; 
}; }' 

is missing the following properties from type 'TransitReport': overdueReviews, outstandingCovenantBreaches, outstandingMarginingBreaches, periodStartDate, periodEndDate

Break your interface TransitReport as below code snippet, because you need to provide overdueReviews, periodEndDate etc.. under data record. Otherwise it'll check those properties under your root TransitReport object, where it's not present.

export interface TransitReport {
    title: string;
    client?: string;
    data: DataRecord;
}

export interface DataRecord {
    overdueReviews: number;
    outstandingCovenantBreaches: number;
    outstandingMarginingBreaches: number;
    periodStartDate: string;
    periodEndDate: string;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM