Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
5 replies
fotoflo

CODEREVIEW - Typescript noob

in javascript i would write this by first defining the empty object and then filling it up, so as to keep my code DRY

typescript doesn't seem to like this... is there a best practice?

export type ParsedLighthouseReport = {
  savingsOpportunities: SavingsOpps[] ;
//  .. a bunch more properties
  error: {
    errorCode: string | null;
    errorMessage: string | null;
  } | null;
};

export default function parseReport(report): ParsedLighthouseReport {
  const parsedReport = {
    error: null,
    savingsOpportunities: null,
   // all the properties: null
  };

  if (report.runtimeError) {
    parsedReport.error = {
      errorCode: report.runtimeError.code,
      errorMessage: report.runtimeError.message,
    };
    return parsedReport;
  }

  parsedReport.savingsOpportunities = getSavingsOpps(report);
  // more properties

  return parsedReport;
}
Was this page helpful?