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

如何使用Flow键入默认导出? Flow有办法实现这个目标吗?

期望的结果:

// 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

可接受的替代方案:

或者,我不介意内联键入每个对象属性,但我认为这在语法上是不可能的:

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

不是我想要的:

我不想做的是将一个变量存储到Flow类型:

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

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

export default myThing

解决方法

你正在做一个 typecast,所以你需要围绕物体的parens,例如更改
export default {
  x: 0,z: {a: 'hello'}
} : Thing

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

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小