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

170
Vistas
Is it valid to override a property in the JavaScript/TypeScript base class with getter/setter pair in the subclass?

Recently, TypeScript has introduced a compilation error, when user tries to override a property in the base class with getter/setter pair (property access augmentation):

class BaseClass {
    prop        : string        = 'base_class'
}

class SubClass extends BaseClass {
    _prop       : string        = 'sub_class'

    /*
       ERROR: 'prop' is defined as a property in class 'BaseClass', but is overridden here in 'SubClass' as an accessor.
    */
    get prop () : string {
        return this._prop
    }
    set prop (value : string) {
        this._prop = value
    }
}
// try running this snippet with `useDefineForClassFields : true` and `useDefineForClassFields : false`
console.log((new SubClass).prop)

This error is motivated by the fact, that in modern JavaScript, class fields have [[DEFINE]] semantic and the above example won't work as intuitively expected if useDefineForClassFields compiler option is set to true.

The property access augmentation pattern

Need to note, that historically nobody was ever using the DEFINE semantic for class properties. Pretty much all the JavaScript and TypeScript code in the world currently assumes the SET semantic for class fields.

Property access augmentation pattern is perfectly valid with SET semantic. Its very idiomatic JavaScript. Its main use case is to trigger some arbitrary code on property read/write. This is very useful for variety of purposes and is done by simply overriding the property of the base class with getter/setter accessors pair.

TypeScript and JavaScript historically were using SET semantic for class fields. After the DEFINE semantic was introduced, TypeScript added the new compiler config, useDefineForClassFields, which controls the semantic of class fields and is disabled by default, because its a breaking change.

QUESTIONS:

  1. Do you think the property access augmentation pattern is valid and should be supported by TypeScript, when class field semantic is [[Set]] (useDefineForClassFields=false which is a default)
  2. Do you use this pattern in your JavaScript/TypeScript codebase? If so, please provide an example of usages as an answer.

See also: Proposal to limit the compilation error to DEFINE semantic only

about 4 years ago · Juan Pablo Isaza
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