Access to static properties via this.constructor in typescript

前端 未结 6 2304
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-18 13:49

I want to write es6 class:

class SomeClass {
    static prop = 123

    method() {
    }
}

How to get access to static prop from <

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-18 14:18

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

提交回复
热议问题