H
H
HiDiv2021-07-06 14:37:08
typescript
HiDiv, 2021-07-06 14:37:08

Why is the variable type lost when nested operations are performed?

There are two interfaces

interface IBigItem {
  fieldName: string
  label: string
  default?: boolean
  ...еще много полей
}
interface ISmallItem {
  fieldName: string
  label: string
  default: boolean
}

There is a large array with data
const columns: IBigItem[]
From it we first get the second array
const selected: ISmallItem = columns.map(column => ({
      fieldName: column.fieldName,
      label: column.label,
      default: column.default ?? false
}))

Then, with selected, a series of operations are performed to change the order (sorting) of elements, and the default property of the elements may change. No new elements are added and nothing is removed.

After that, we need to coerce the columns array into the same element order as selected and update the default property of selected.
const newColumns = selected.map(selectedColumn => {
  const newColumn = columns.find(columnDef => columnDef.fieldName === selectedColumn.fieldName)
  newColumn.default = selectedColumn.default
  return  newColumn
})


On the line columnDef.fieldName === selectedColumn.fieldName, the error "Unsafe member access .fieldName on an `any` value" pops up over selectedColumn.fieldName

And on the line newColumn.default = selectedColumn.default, the error "Unsafe assignment of an `any` value" " over newColumn.default and "Unsafe member access .fieldName on an `any` value" over selectedColumn.default.

What is the problem I can not understand, tk. in the IDE, when hovering over the selectedColumn, it shows the correct type of ISmallItem ... I tried to explicitly specify types through as and make temporary variables, nothing helps.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question