Access to static properties via this.constructor in typescript

℡╲_俬逩灬. 提交于 2019-12-09 14:10:23

问题


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


回答1:


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




回答2:


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



回答3:


It's a bit dirty, but this code works for me in the Typescript Playground:

class SomeClass {
    static prop = 123;

    constructor() {
        console.log(this.constructor["prop"]);
    }

}

var a = new SomeClass();



回答4:


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.




回答5:


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



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


来源:https://stackoverflow.com/questions/33387318/access-to-static-properties-via-this-constructor-in-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!