2017-06-05 7 views
1

私は深くネストされたオブジェクトであるフロータイプを持っています。私は、そのプロパティは、その型のオブジェクトに存在する場合、特定のプロパティを取り出す関数を作成したい、とそのプロパティの値は、その型である:トライフロー上のFacebook Flow - 深くネストされたオブジェクトから型キャストされたプロパティを返す方法

表示可能here

/* @flow */ 

type DeeplyNested = { 
    [string]: string | DeeplyNested 
}; 

function getDogs(deeplyNested: DeeplyNested): ?DeeplyNested { 
    return (deeplyNested.dogs: DeeplyNested); 
} 

let someThingWithDogs = { 
    dogs: { 
    somethingElse: 'yes' 
    } 
} 

let result = getDogs(someThingWithDogs); 

私が手エラー:

return (deeplyNested.dogs: DeeplyNested); ^string.

This type is incompatible with return (deeplyNested.dogs: DeeplyNested); ^object type

プロパティがそのタイプのものであるかどうかを最初にチェックし、そのプロパティをキャストして戻す方法はありますか?

答えて

0

はちょうどそれがオブジェクトだかどうかを確認する必要があり、それを考え出した:

function getDogs(deeplyNested: DeeplyNested): ?DeeplyNested { 
    if (typeof deeplyNested.dogs === 'object') return deeplyNested.dogs; 
} 
関連する問題