Artur Eshenbrener
Artur Eshenbrener

Reputation: 2030

Access to static properties via this.constructor in typescript

I want to write es6 class:

class SomeClass {
    static prop = 123

    method() {
    }
}

How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this.constructor, but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'".

Upvotes: 42

Views: 20011

Answers (5)

ktretyak
ktretyak

Reputation: 31779

I guess that you want in the future extends this class. So it is better to do this:

class SomeClass<T extends typeof SomeClass = typeof SomeClass> {
    static prop = 123

    method() {
        (this.constructor as T).prop;
    }
}

Upvotes: 6

Okku
Okku

Reputation: 7819

Accessing static properties through this.constructor (as opposed to just doing SomeClass.prop like normally you would) is only ever useful when you don't know the name of the class and have to use this instead. typeof this doesn't work, so here's my workaround:

class SomeClass {

  static prop = 123;

  method() {

    const that = this;

    type Type = {
      constructor: Type;
      prop: number; //only need to define the static props you're going to need
    } & typeof that;

    (this as Type).constructor.prop;
  }

}

Or, when using it outside the class:

class SomeClass {
  static prop = 123;
  method() {
    console.log(
      getPropFromAnyClass(this)
    );
  }
}

function getPropFromAnyClass<T>(target: T) {
  type Type = {
    constructor: Type;
    prop: number; //only need to define the static props you're going to need
  } & T;

  return (target as Type).constructor.prop;
}

Upvotes: 3

colder
colder

Reputation: 744

Microsoft programmer talking this but there is not a good way to type constructor. You can use this tip first.

class SomeClass {
    /**
     * @see https://github.com/Microsoft/TypeScript/issues/3841#issuecomment-337560146
     */
    ['constructor']: typeof SomeClass

    static prop = 123

    method() {
        this.constructor.prop // number
    }
}

Upvotes: 30

pixelpax
pixelpax

Reputation: 1517

Usually the simple way is:

class SomeClass {
    static prop = 123

    method() {
        console.log(SomeClass.prop)  //> 123
    }
}

Note that if you use this, subclasses of SomeClass will access the SomeClass.prop directly rather than SomeSubClass.prop. Use basarat's method if you want subclasses to access their own static properties of the same name.

Upvotes: -2

basarat
basarat

Reputation: 276303

but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'".

Typescript does not infer the type of constructor to be anything beyond Function (after all ... the constructor might be a sub class).

So use an assertion:

class SomeClass {
    static prop = 123;
    method() {
        (this.constructor as typeof SomeClass).prop;
    }
}

More on assertions

Upvotes: 41

Related Questions