How to flow type a 'require('')' image?

Viewed 874

I have an object that looks like this with a flow type. I checked what the require function is from hovering in Vscode. But not sure here to access it from? I'm wondering if I need something like flow-typed. But can't find it there. Am i typing this correctly?

// @flow
const backgroundImage = require('./myBg.jpeg')

type configType = {
  backgroundImage: string | NodeRequire
}

const config = {
  backgroundImage
}
1 Answers

If you are using webpack and url loader the image would typically be a string. You can use the name_mapper.extension option of flow to automatically type all jpeg (and also other files if you extend the regex) file imports as strings:

# .flowconfig
[options]
module.name_mapper.extension='jpeg' -> '<PROJECT_ROOT>/ImageFlowStub.js.flow'

and then create ImageFlowStub.js.flow file in your project root (or really anywhere else and adjust the path in the .flowconfig:

/* @flow */

declare export default string;

See also this question: Flow “Required module not found” when importing a CSS file

Related