繁体   English   中英

如何在 js 中使用流在对象文字中指定它

[英]How to specify this in an object literal with flow in js

我正在使用// @flow strict但不知何故它在使用this时无法在对象文字中正常工作。 this似乎被解释为任何。

这是示例代码

type TestType = {
  arr: Array<number>,
  fun: () => void,
}

const testObject: TestType = {
  arr:[],
  fun(){
    this.arr.toUpperCase();
  }
}

testObject.fun();

我如何告诉 flow 它知道this.arr.toUpperCase()不存在,因为this.arr是一个数组?

似乎没有办法在函数中明确定义this类型:

在 Flow 中,您不键入 annotate this,Flow 将检查您调用函数的任何上下文。

作为一种解决方法,您可以执行以下操作

const testObject: TestType = {
  arr:[],
  fun(){
    const {arr}: TestType = this;
    arr.toUpperCase();
  }
}

testObject.fun(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM