如何使用Flow键入默认导出? [英] How can I type a default export using Flow?

查看:110
本文介绍了如何使用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



可接受的选择:



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

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
}



不是我想要的:



想要做的是将变量存储到Flow类型:

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


推荐答案

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

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

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

这篇关于如何使用Flow键入默认导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆