簡體   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