javascript - 即使在 if 语句中包装后,Flow 仍认为该值为 null

标签 javascript reactjs flowtype

this.collapsible 是类型为 ?HTMLElement 的 React Ref。我正在尝试像这样设置 style:

this.collapsible.style.height = ...

而且,尽管将上面的内容包装在 if (this.collapsible && this.collapsible.style) 中,似乎 flow 仍然相信 this.collapsible.style可以为空。

Cannot get this.collapsible.style because property style is missing in null or undefined [1].

      97│
      98│   componentDidMount() {
      99│     if (this.collapsible && this.collapsible.style) {
     100│       this.collapsible.style.height = getInitialHeight(this.props);{
        :
 [1] 148│   collapsible: ?HTMLElement;

如何解决?

最佳答案

正如@John Ruddell 指出的那样,给变量起别名是可行的。

if (this.collapsible) {
  const collapsible: HTMLElement = this.collapsible;
  collapsible.style.height = getInitialHeight(this.props);

我无法找到无需创建新变量即可转换类型的好方法。

关于javascript - 即使在 if 语句中包装后,Flow 仍认为该值为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434877/

相关文章:

javascript - 我如何纠正 $injector :unpr error with Angular 1. 6/MVC 4.6/UI-Grid

javascript - Jquery 检查类名是否包含字符,例如 "Y"或 "N"

reactjs - React Redux - 在连接的组件中使用 mapDispatchToProps 时如何在 componentDidMount 上分派(dispatch)操作

javascript - React 中的绿色复选标记动画

javascript - 条件对象属性的流类型问题

javascript - 将选定的文本扩展为整个单词

javascript - ExtJs - onReady 是 tab click()

reactjs - MUI : How to update the DataGrid state programmatically?

redux - 为 Redux 操作使用 Flow 联合类型

typescript - 如何通过 Typescript for Flow 导入 React-Native 类型?