facebook / facebook/flow

Map over an object and using/retaining the type of its properties

Đang mở
#3,140 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
question
Ngôn ngữ chính
Rust
Star
22.3k
Fork
1.9k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Here's the gist, I have an object that contains functions (selectors), that take a given object (let's call it X (the same for all these selectors) and map to a subpart of this object. I would like to iterate over this object, to create new functions that take an object containing X, and still returning the subpart of their subobject.

```javascript
type LocalState = {a: A}
type GlobalState = {g: LocalState};
// from
const sels = {
getA: (localState: LocalState): A => localState.a,
};

// to
const remappedSels = {
getA: (globalState: GlobalState): A => sels.getA(globalState.g),
};
```

I managed to keep the typings as long as I manually remap each function of sels, which is sadly not enough for me; I tried to have a look to the typings of ramda `mapObjIndexed`, but it's not precise enough. Using `Objet.keys(sel).reduce(...)` does not seem to work either, it loses the types. Does anyone have an idea on how to do so?

Here is the flowtry [example](https://flowtype.org/try/#0PTAEAEDMBsHsHcBQBjWA7AzgF1AczgEYCG0AylkVgKagC8oA3oqKEQFyPMugBuJAjBwDkRIQBouLPtA6QSGKhJYBfCcoDciFOmx4qWfnVAAKDAEo6APlAYAdNP6bUmHLn0A1EkdMXa1u9Ka2i6gcMgkpFTQVMhYsABOGIb0DG4GYnpYntAaWs66aaQArgTklFTJJuZWNrZEQSCgAGLxsAC2oADiNQCCrGgAJqB9fqAAGqBxXTVjwbrxVG1EAA5GADydGT0ZY5bGhSVl1Bzdoz1mHMbGI9Zjvtant-dcplEcN+MXJo+fVpJVJ3uNii+30xVKFGoPjMmm4cNAiAGMWgRAWoEgRTQsQAluhQEtlgB5AgAKwAkoMqAAPKgDNbbABCe0gaEu0neGQA1lQAJ4cbDxbFoXAZWAcYkkmJYIEM0UcBgAbW5fJsWEFwoAuu9lF9Fcr+WqhbgtaAGbl4dxEI0ABIISawUADB1YAAW2IwoGWqJwy1auHiRCWWGx4Wg0B5GQJyyNoDxrpozmoaBwsEgCMa+RwCyjtMi0EqBIlFKRNIGxmzK1BWHBRwqZgyYQiUSlCSSMLmWcWK2WuailSYLDSglAFeWVZrkLrxkbZGbsVb-FsQ-rXDS2Q4o-Hh0n-DM09gobzLcSi7XJBXuTyOhwGAN6twRlHPYGedP+n4+0IEUn7czoCo8StPEHBoEUbQEABj5dssz6vku76frAxBkD+TjXqABAcAQsCwNAUE5i+fbwVkJCIchtYwkAA)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.