Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct flow type for window object?

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
  }
  componentDidMount() {
    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.com/flow-type-cheat-sheet/latest/#lib/bom.js

like image 343
Pratik Bothra Avatar asked Aug 18 '17 09:08

Pratik Bothra


2 Answers

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

like image 103
Saad Avatar answered Oct 25 '22 23:10

Saad


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!

like image 22
Adam Avatar answered Oct 25 '22 23:10

Adam