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

284
Vistas
TypeScript why use traditional classes instead of interfaces?

I can do this:

enum DogsKinds {
  Labrador,
  Aski
}

class Dog {
  name: string;
  kind: DogKinds;
  constructor() {}
}

And then init a dog object:

const dog = new Dog();

However I can do the same with interface:

interface Dog {
  name: string;
  kind: DogsKind;
}

const dog: Dog = {
  name: 'some name',
  kind: DogsKinds.Labrador
}

And get the same result - easier.

What are the differences, when to use interface and when to use classes, is there a best practice or rule of thumb?

It seems that interfaces are easier to work with, if I wrong please correct me.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think one important difference is finding out at runtime which kind of object you are dealing with when using inheritance.

With classes you can use the instanceof operator:

if (animal instanceof Dog) { /* ... */ }

When you are just using plain objects with TypeScript you will have to supply an additional property to differentiate the types during runtime.

type AnimalType = 'dog' | 'cat'

interface Dog extends Animal {
  type: 'dog'
  name: string;
  kind: DogsKind;
}

if (animal.type === 'dog') { /* ... */ }
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