4

初期データを作業データに変換する必要があります。どちらも独自のタイプを持っていますが、唯一の違いは、初期データでは名前がオプションであることです。作業データを作成するときに空の名前にデフォルト値'__unknown__'を使用します。ここオプションのプロパティを持つオブジェクトでスプレッド演算子を使用するとエラーが発生する

は、例えば、コードされ:working1を初期化上記の例の終わり

/* @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 
} 

OKであるが、working2を初期化し、オブジェクトの拡散演算子を使用して、このエラーを表示するflowtypeを引き起こす:

4: name?: string 
      ^undefined. This type is incompatible with 
8: name: string 
     ^string 

私は普及したオペレータがこれをいかに引き起こすことができるかわかりません。誰もそれを説明できますか?ありがとうございました。

"Working" example on https://flowtype.org/try/... is here

答えて

1

a lot of bugs about the spread operatorがあります。あなたのケースは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 
} 

この設定を01に追加してください:

[options] 
suppress_comment=.*\\$FlowIssue 

これはエラーを抑制します。

関連する問題