Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
Typescript array semantic error TS2532: Object is possibly 'undefined'

// I'm using Typescript 4.4.4 and node.js 12 LTS for this project, the Matrix class is giving me a problem during compilation with rollup, I'm not sure why, everything looks ok to me

Error:/src/Matrix.ts(29,9): semantic error TS2532: Object is possibly 'undefined'.

The source code:

export class Matrix {
  rows: number;
  cols: number;
  data: number[][];
  constructor(rows: number, cols: number) {
    this.rows = rows;
    this.cols = cols;
    this.data = Array(this.rows)
      .fill(1)
      .map(() => Array(this.cols).fill(0));
  }

  copy() {
    const m = new Matrix(this.rows, this.cols);
    for (let i = 0; i < this.rows; i++) {
      for (let j = 0; j < this.cols; j++) {
        m.data[i][j] = this.data[i][j];
      }
    }
    return m;
  }
...
}

this is the line with the error:

  m.data[i][j] = this.data[i][j];

and:

 m.data[i][j] = this?.data?.[i]?.[j] as number;

seemed to get rid of the red underline on the right side, but it doesn't work on he left side

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

use ! for i'm sure, typescript, you don't need to check undefined

 m.data[i]![j]! = this.data[i]![j]!;

you've got this warning because of the noUncheckedIndexedAccess tsconfig setting: https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda