最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the correct flow type for window object? - Stack Overflow

programmeradmin3浏览0评论

I need to pass in window from the root of my application, and I'm confused as to what flow type I should be using.

I tried

export default class ListAttribute extends Component {
  props: {
   frameWindow: mixed
  }
  ponentDidMount() {
    this.props.frameWindow.addEventListener('click', this.closeList, false) 
  }
  ....
}

This gives me call of method addEventListener. Method cannot be called on mixed, I tried refinement to no luck.

I tried looking here, but couldn't find anything for the bom itself. .js

I need to pass in window from the root of my application, and I'm confused as to what flow type I should be using.

I tried

export default class ListAttribute extends Component {
  props: {
   frameWindow: mixed
  }
  ponentDidMount() {
    this.props.frameWindow.addEventListener('click', this.closeList, false) 
  }
  ....
}

This gives me call of method addEventListener. Method cannot be called on mixed, I tried refinement to no luck.

I tried looking here, but couldn't find anything for the bom itself. https://www.saltycrane./flow-type-cheat-sheet/latest/#lib/bom.js

Share Improve this question asked Aug 18, 2017 at 9:27 Pratik BothraPratik Bothra 2,7042 gold badges33 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

There currently isn't any typings for the window object it seems. For now, it looks like the type of any is used.

Do you need any methods that are specific to window? If all you're doing is the one call to addEventListener, then that can be called on any EventTarget, which window certainly is. And since the default type for window is any, you should be able to pass it in for an EventTarget.

Here's a simpler example that hopefully shows that idea, without bringing in all the details of your code:

function withWindow(window: EventTarget) {
  window.addEventListener("click", (e: MouseEvent) => console.log(e), false); 
}

withWindow(window); // type checks fine!

Hope that helps!

发布评论

评论列表(0)

  1. 暂无评论