Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda