42

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'".

4
  • Have you tried 'this.constructor["prop"]'? Commented Oct 28, 2015 at 10:20
  • This is not a solution: I have completely miss type chek in that case. I want to get error on access non existing property. Commented Oct 28, 2015 at 10:24
  • 1
    Can you share your actual code? In what context are you trying to do it? Commented Oct 28, 2015 at 11:47
  • 3
    @ArturEshenbrener you could do (this.constructor as typeof SomeClass).prop, but what's the point? Why not do SomeClass.prop? Commented Oct 28, 2015 at 14:25

5 Answers 5

42

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

Sign up to request clarification or add additional context in comments.

1 Comment

I use self as static context like PHP const self = this.constructor as typeof SomeClass;
31

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
    }
}

1 Comment

It's worth noting though that you should not invoke the constructor if you do this, since a subclass doesn't necessarily have the same signature.
6

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;
    }
}

Comments

3

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;
}

1 Comment

Presumably you lean on this.constructor so as to not break inheritance, no?
-2

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.

1 Comment

The question states "without use SomeClass explicitly". I'm confused on how this provides an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.