facebook / facebook/flow

Ramda.assoc cannot be typed correctly

Aperta
#4,748 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

[`Ramda.assoc()`](http://ramdajs.com/docs/#dissoc), by analogy with Clojure's `assoc`, takes a key, a value, and an object, and returns a copy of the object with that key and value added. (Ramda functions are also curried, but that's orthogonal to this issue.)

The most reasonable way to type `assoc()` I could come up with is the following, which doesn't work correctly:

```js
declare function assoc(key: K, val: V, src: O): O & { [K]: V };

let assocd = assoc('a', 'one', { b: 2 });
(assocd.a: string);
(assocd.b: number);
// This should be an error, but it's not, because the type of `assocd` now has
// an indexer property `{[string]: string}`.
(assocd.foo: string);
```

It appears that `K` becomes bound to `string`, and not the more specific type `'a'`. Notice that constraining `K` to `'a'` fixes the problem:

```js
declare function assoc(key: K, val: V, src: O): O & { [K]: V };

let assocd = assoc('a', 'one', { b: 2 });
(assocd.a: string);
(assocd.b: number);
// $ExpectError: This is now an error, because the type of `assocd` no longer
// uses an indexer property.
(assocd.foo: string);
```

I'm not certain where or if there's a shortcoming in Flow here, but it would be nice to be able to type `assoc` properly.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.