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

148
Visualizações
What is the best approach to declare variable in typescript for class assignment?

I don't want to declare accountHandler as any it is not good practice. What is a better approach to create class variable when you have to assign it to class in the constructor.

main.ts

{
 private accountHandler: any= {};
 private requestLOB: string[] = [];


    constructor() {
        if (process.env.ENABLEBackendSwitch === "true") {
            this.accountHandler = new AccountBalanceHandlerV2();
        } else {
            this.accountHandler = new AccountBalanceHandler();
        }
    }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Instead of any you can use AccountBalanceHandlerV2 | AccountBalanceHandler. But you actually don't even need to set the type as TypeScript will infer the value for you based on what you assign it to in the constructor (so long as you remove your default assignment):

class A {
  private accountHandler;


  constructor() {
    if (process.env.ENABLEBackendSwitch === "true") {
      this.accountHandler = new AccountBalanceHandlerV2();
    } else {
      this.accountHandler = new AccountBalanceHandler();
    }
  }
}

accountHandler is inferred to be of type AccountBalanceHandlerV2 | AccountBalanceHandler.

about 4 years ago · Juan Pablo Isaza Relatório

0

Typescript will actually infer the type of fields initialized in constructors. So if you don't have any annotation, it will be infered.

class X {
    private accountHandler;
//           ^?  X.accountHandler: AccountBalanceHandlerV2 | AccountBalanceHandler
    constructor() {
        if (process.env.ENABLEBackendSwitch === "true") {
            this.accountHandler = new AccountBalanceHandlerV2();
        } else {
            this.accountHandler = new AccountBalanceHandler();
        }
    }
}

Playground Link

But the best practice is to actually be explicit about the type:

class X {
    private accountHandler: AccountBalanceHandlerV2 | AccountBalanceHandler
    constructor() {
        if (process.env.ENABLEBackendSwitch === "true") {
            this.accountHandler = new AccountBalanceHandlerV2();
        } else {
            this.accountHandler = new AccountBalanceHandler();
        }
    }
}

Playground Link

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