Jos*_*ley 14 javascript ecmascript-6 flowtype
// 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)
| 归档时间: |
|
| 查看次数: |
3255 次 |
| 最近记录: |