0

I'm sort of a newbie to typescript, i've danced around it long enough

I have two interfaces, one is reading from a database that I dont have control over and cannot trust that the values it returns are what I expect them to be.

// pulled from database
interface JobTableRow {
  job_number: string,
  description: string | null,
  project_manager: string | null,
  status: string | null,
  ts_customer_id: string | null,
  completed_date: string | null,
  date_stamp: string,
  time_stamp: string,
  contract_base: number,
  contract_approved_changes: number,
  contract_revised_total: number,
  jtd_cost: number,
  start_date: string | null,
  labor_tax_group: string | null,
  material_tax_group: string | null,
  subcontract_tax_group: string | null,
  equipment_tax_group: string | null,
  overhead_tax_group: string | null,
  other_tax_group: string | null,
  ts_row_id: string,
  ts_row_version: string
}
// manually calculated field after being pulled from the database
interface JobMapping extends JobTableRow {
  ts_updated: string
}

an example record:

const mapped: JobMapping = {
  job_number: '0000',
  description: 'some project',
  project_manager: 'some person',
  status: 'active',
  date_stamp: '2020-04-20',
  time_stamp: '11:12:57',
  labor_tax_group: '',
  material_tax_group: '',
  subcontract_tax_group: '',
  equipment_tax_group: '',
  overhead_tax_group: '',
  other_tax_group: '',
  // ...
  ts_updated: '2020-04-20T18:12:57.000Z'
}

what I am used to doing is something like this in nodejs in order to convert the empty string values to null, but typescript yells at me saying Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'JobMapping'. No index signature with a parameter of type 'string' was found on type 'JobMapping'.ts(7053)

Object.keys(mapped).forEach(key => {
  if (typeof mapped[key] === 'string') {
    let val: string = mapped[key]
    if (val === '') mapped[key] = null
  }
})
1

1 Answer 1

1

You can fix the error by doing this:

let mapped: JobMapping = {
  job_number: '0000',
  description: 'some project',
  project_manager: 'some person',
  status: 'active',
  date_stamp: '2020-04-20',
  time_stamp: '11:12:57',
  labor_tax_group: '',
  material_tax_group: '',
  subcontract_tax_group: '',
  equipment_tax_group: '',
  overhead_tax_group: '',
  other_tax_group: '',
  // ...
  ts_updated: '2020-04-20T18:12:57.000Z'
}

Object.keys(mapped).forEach(key => {
  const keyWithType = key as keyof typeof mapped;
  let value = mapped[keyWithType];
  if (typeof value === 'string' && value === "") {
    mapped = {...mapped, [keyWithType]: null};
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.