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

Jos*_*ley 14 javascript ecmascript-6 flowtype

如何使用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
Run Code Online (Sandbox Code Playgroud)

可接受的替代方案:

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

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

不是我想要的:

希望做的是存储变量只是对流动键入:

// 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
Run Code Online (Sandbox Code Playgroud)

log*_*yth 10

你正在做一个类型转换,所以你需要围绕对象的parens,例如更改

export default {
  x: 0,
  y: true,
  z: {a: 'hello'}
} : Thing
Run Code Online (Sandbox Code Playgroud)

export default ({
  x: 0,
  y: true,
  z: {a: 'hello'}
} : Thing)
Run Code Online (Sandbox Code Playgroud)