在具有可选 属性 的对象中使用扩展运算符时出错
Error when using the spread operator in objects with optional property
我想把初始数据变成工作数据。两者都有自己的类型,唯一的区别是在初始数据中,名称是可选的。当我创建工作数据时,我使用默认值 '__unknown__'
作为空名称。
示例代码如下:
/* @flow */
type NAME_OPTIONAL = {
name?: string
}
type NAME_MANDATORY = {
name: string
}
type INITIAL = {
source: string,
data: NAME_OPTIONAL[] // <-- Here the names are OPTIONAL.
}
type WORKING = {
source: string,
data: NAME_MANDATORY[] // <-- Here the name are MANDATORY.
}
// We have some initial data.
const initial: INITIAL = {
source: 'some.server.com',
data: [{ name: 'Adam' }, { name: undefined }]
}
// And we want to turn initial data into working data.
const workingData = initial.data.map((i) => {
return {
name: i.name || '__unknown__'
}
});
// This is OK:
const working1: WORKING = {
source: initial.source,
data: workingData
}
// This is NOT OK:
const working2: WORKING = {
...initial,
data: workingData
}
在上面示例的末尾初始化 working1
是可以的,但是使用对象展开运算符初始化 working2
会导致 flowtype 显示此错误:
4: name?: string
^ undefined. This type is incompatible with
8: name: string
^ string
我不明白传播运算符怎么会导致这种情况。谁能解释一下?谢谢你。
有a lot of bugs about the spread operator. Your case seems identical to this one.
在他们修复之前可能没有解决方案,除了将 …
运算符替换为 Object.assign
:
const working2: WORKING = Object.assign({}, initial, { data: workingData })
如果还是不行,您可以在行上方添加注释:
// $FlowIssue
const working2: WORKING = Object.assign({}, initial, { data: workingData })
或:
// $FlowIssue
const working2: WORKING = {
...initial,
data: workingData
}
然后在您的 .flowconfig
中添加此设置:
[options]
suppress_comment=.*\$FlowIssue
这将抑制错误。
我想把初始数据变成工作数据。两者都有自己的类型,唯一的区别是在初始数据中,名称是可选的。当我创建工作数据时,我使用默认值 '__unknown__'
作为空名称。
示例代码如下:
/* @flow */
type NAME_OPTIONAL = {
name?: string
}
type NAME_MANDATORY = {
name: string
}
type INITIAL = {
source: string,
data: NAME_OPTIONAL[] // <-- Here the names are OPTIONAL.
}
type WORKING = {
source: string,
data: NAME_MANDATORY[] // <-- Here the name are MANDATORY.
}
// We have some initial data.
const initial: INITIAL = {
source: 'some.server.com',
data: [{ name: 'Adam' }, { name: undefined }]
}
// And we want to turn initial data into working data.
const workingData = initial.data.map((i) => {
return {
name: i.name || '__unknown__'
}
});
// This is OK:
const working1: WORKING = {
source: initial.source,
data: workingData
}
// This is NOT OK:
const working2: WORKING = {
...initial,
data: workingData
}
在上面示例的末尾初始化 working1
是可以的,但是使用对象展开运算符初始化 working2
会导致 flowtype 显示此错误:
4: name?: string
^ undefined. This type is incompatible with
8: name: string
^ string
我不明白传播运算符怎么会导致这种情况。谁能解释一下?谢谢你。
有a lot of bugs about the spread operator. Your case seems identical to this one.
在他们修复之前可能没有解决方案,除了将 …
运算符替换为 Object.assign
:
const working2: WORKING = Object.assign({}, initial, { data: workingData })
如果还是不行,您可以在行上方添加注释:
// $FlowIssue
const working2: WORKING = Object.assign({}, initial, { data: workingData })
或:
// $FlowIssue
const working2: WORKING = {
...initial,
data: workingData
}
然后在您的 .flowconfig
中添加此设置:
[options]
suppress_comment=.*\$FlowIssue
这将抑制错误。