【问题标题】:Access to static properties via this.constructor in typescript通过 typescript 中的 this.constructor 访问静态属性
【发布时间】:2016-01-28 00:08:50
【问题描述】:

我要写es6类:

class SomeClass {
    static prop = 123

    method() {
    }
}

如何在不显式使用SomeClass 的情况下从method() 访问静态prop?在 es6 中可以使用 this.constructor 完成,但在 typescript 中 this.constructor.prop 会导致错误“TS2339: 类型 'Function' 上不存在属性 'prop'”。

【问题讨论】:

  • 你试过'this.constructor["prop"]'吗?
  • 这不是一个解决方案:在这种情况下,我完全错过了类型 chek。我想在访问不存在的属性时出错。
  • 你能分享你的实际代码吗?您在什么情况下尝试这样做?
  • @ArturEshenbrener 你可以做(this.constructor as typeof SomeClass).prop,但有什么意义呢?为什么不SomeClass.prop

标签: typescript this static-methods static-members typescript1.6


【解决方案1】:

但在 typescript 中 this.constructor.prop 导致错误“TS2339: 属性 'prop' 在类型 'Function' 上不存在”。

Typescript 不会将 constructor 的类型推断为 Function 之外的任何类型(毕竟......构造函数可能是一个子类)。

所以使用断言:

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

More on assertions

【讨论】:

    【解决方案2】:

    Microsoft 程序员在讨论这个问题,但没有输入constructor 的好方法。你可以先使用这个技巧。

    class SomeClass {
        /**
         * @see https://github.com/Microsoft/TypeScript/issues/3841#issuecomment-337560146
         */
        ['constructor']: typeof SomeClass
    
        static prop = 123
    
        method() {
            this.constructor.prop // number
        }
    }
    

    【讨论】:

      【解决方案3】:

      我猜你想在未来扩展这个类。所以最好这样做:

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

      【讨论】:

        【解决方案4】:

        通常简单的方法是:

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

        请注意,如果您使用它,SomeClass 的子类将直接访问 SomeClass.prop 而不是 SomeSubClass.prop。如果您希望子类访问它们自己的同名静态属性,请使用 basarat 的方法。

        【讨论】:

        • 问题说明“不明确使用 SomeClass”。我对这如何提供答案感到困惑。
        【解决方案5】:

        通过this.constructor 访问静态属性(而不是像通常那样只做SomeClass.prop)只有在您不知道类的名称而必须使用this 时才有用。 typeof this 不起作用,所以这是我的解决方法:

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

        或者,在课外使用时:

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

        【讨论】:

          猜你喜欢
          • 2012-03-30
          • 1970-01-01
          • 2018-04-18
          • 1970-01-01
          • 1970-01-01
          • 2014-04-25
          • 2017-01-26
          • 1970-01-01
          • 2021-02-05
          相关资源
          最近更新 更多