ExpertiDict

TypeScript · Intermediário · Inglês · Técnico

strict property initialization

/stɹɪkt ˈpɹɑpɚti ɪˌnɪʃələˈzeɪʃən/substantivo

Flag strictPropertyInitialization que exige que todo campo de classe, sem undefined no tipo, seja inicializado na declaração ou no construtor. Depende de strictNullChecks.

Onde usar

O erro 'has no initializer and is not definitely assigned' aparece em classes de entidades e de injeção. As saídas são inicializar, aceitar undefined ou usar o ! de definite assignment.

Em código

class Pedido {
  // id: number; // erro: sem inicializador
  nome = 'novo'; // ok
  obs?: string; // ok, é opcional
  dono!: string; // ok, promessa externa
  constructor() {}
}

Exemplos

Strict property initialization flagged the field with no initializer.

O strict property initialization apontou o campo sem inicializador.

Initialize it in the constructor or mark it optional.

Inicialize no construtor ou marque como opcional.

Cuidado

Atribuir o campo em outro método, como init(), não satisfaz a checagem, porque o compilador só considera o construtor.

Sinônimos

strictPropertyInitialization

Veja também

Todos os termos de TypeScript