admin管理员组

文章数量:1356745

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!

本文标签: