facebook / facebook/flow

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

Ouverte
#3,140 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
question
Langage dominant
Rust
Étoiles
22.3k
Forks
1.9k
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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)

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.