// 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,
y: boolean,
z: complexThing
}
const myThing: Thing = {
x: 0,
y: true,
z: {a: 'hello'}
}
export default myThing
答案 0 :(得分:10)
您正在进行typecast,因此您需要在对象周围使用parens,例如变化
export default {
x: 0,
y: true,
z: {a: 'hello'}
} : Thing
到
export default ({
x: 0,
y: true,
z: {a: 'hello'}
} : Thing)