[英]How to specify this in an object literal with flow in js
I am using // @flow strict
but somehow it does not work properly in an object literal when using this
.我正在使用
// @flow strict
但不知何故它在使用this
时无法在对象文字中正常工作。 this
seemed to be interpreted as any. this
似乎被解释为任何。
This is the example code这是示例代码
type TestType = {
arr: Array<number>,
fun: () => void,
}
const testObject: TestType = {
arr:[],
fun(){
this.arr.toUpperCase();
}
}
testObject.fun();
How can I tell flow that it knows that this.arr.toUpperCase()
is not existing, because this.arr
is an Array?我如何告诉 flow 它知道
this.arr.toUpperCase()
不存在,因为this.arr
是一个数组?
There seems to no way to explicitly define this
type in function:似乎没有办法在函数中明确定义
this
类型:
In Flow you don't type annotate this and Flow will check whatever context you call the function with.
在 Flow 中,您不键入 annotate this,Flow 将检查您调用函数的任何上下文。
As a workaround you can do something like: :作为一种解决方法,您可以执行以下操作:
const testObject: TestType = {
arr:[],
fun(){
const {arr}: TestType = this;
arr.toUpperCase();
}
}
testObject.fun();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.