MapUtil: permit read-only input to fromObject (#1676)

Summary:
Because `fromObject` does not mutate its input, it’s safe to accept
read-only inputs. This is needed for parts of the CredRank Markov
process graph implementation.

Test Plan:
Typing test added.

wchargin-branch: maputil-readonly-input
This commit is contained in:
William Chargin 2020-02-24 20:44:11 -08:00 committed by GitHub
parent 976eaf05ec
commit 1c182d4121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View File

@ -21,7 +21,7 @@ export function toObject<K: string, V, InK: K, InV: V>(
* iteration order, as returned by `Object.keys`.
*/
export function fromObject<K, V, InK: K & string, InV: V>(object: {
[InK]: InV,
+[InK]: InV,
}): Map<K, V> {
const result = new Map();
const keys = (((Object.keys(object): string[]): any): InK[]);

View File

@ -71,6 +71,10 @@ describe("util/map", () => {
const o: {|[string]: number|} = ({a: 1}: any);
const _: Map<string, number> = MapUtil.fromObject(o);
});
it("can accept a read-only object", () => {
const o: {+[string]: number} = {a: 1};
const _: Map<string, number> = MapUtil.fromObject(o);
});
it("statically rejects a map with keys not a subtype of string", () => {
const input: {[number]: string} = {};
input[12] = "not okay";