Search code examples
typescriptclassextendssuper

Typescript : How to overload a protected class constant


With typescript, when defining classes with inheritance, is it possible to overload a protected constant ?

Exemple :

class A {
   static defaultProps = {
      message: ''
   }
}

class B extends A {
   static defaultProps = {
      // get defaultProps from A,
      title: ''
   }
}

I don't know if it's important that I'm working with React.


Solution

  • You should just extend the defaultProps prop from A class (note: the defaultProps is ˙static` so you can access that on the class not on the instance)

    The following should work:

    class A {
       static defaultProps = {
          message: ''
       }
    }
    
    class B extends A {
       static defaultProps = {
          ...A.defaultProps,
          title: ''
       }
    }