Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I type a default export using Flow?

How can I type a default export using Flow? Does Flow have a way of accomplishing this?

Desired Outcome:

// index.js
type complexThing = {
  a: string
}
type Thing = {
  x: number,
  y: boolean,
  z: complexThing
}

export default {
 x: 0,
 y: true,
 z: {a: 'hello'}
} : Thing // this says my default export is a Thing

Acceptable Alternative:

Alternatively, I wouldn't mind typing each of the object properties inline, but I think that's syntactically impossible:

export default {
 // I don't know how to add type signatures here
 x: 0, // number
 y: true, // boolean
 z: {a: 'hello'} // complexThing
}

Not What I Want:

What I don't want to do is store a variable just to to Flow type it:

// index.js
type complexThing = {
  a: string
}
type Thing = {
  x: number,
  y: boolean,
  z: complexThing
}

const myThing: Thing = {
 x: 0,
 y: true,
 z: {a: 'hello'}
}

export default myThing
like image 977
Joseph Fraley Avatar asked Feb 02 '17 00:02

Joseph Fraley


People also ask

What is a default export?

Export default is used when there is only one export to be made from a particular file and when importing this one element, we don't have to worry about giving the same name to our import. This combination of export and import allows us to implement modularity.

What does this line mean export default table?

Export default means you want to export only one value the is present by default in your script so that others script can import that for use.

How does export differ from export default?

The difference between export default and export as default export { variable as default } exports the reference to the exported variable , whereas with export default variable , the importing modules do not receive the reference to the exported variable .


1 Answers

You are doing a typecast so you will need parens around the object, e.g. change

export default {
  x: 0,
  y: true,
  z: {a: 'hello'}
} : Thing

to

export default ({
  x: 0,
  y: true,
  z: {a: 'hello'}
} : Thing)
like image 62
loganfsmyth Avatar answered Sep 28 '22 05:09

loganfsmyth