简体   繁体   English

如何使用Flow键入默认导出?

[英]How can I type a default export using Flow?

How can I type a default export using Flow? 如何使用Flow键入默认导出? Does Flow have a way of accomplishing this? Flow有办法实现这个目标吗?

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

You are doing a typecast so you will need parens around the object, eg change 你正在做一个类型转换,所以你需要围绕对象的parens,例如更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM