"string with unknown value" error, but value is known (restricted by type and also fixed by code)
Flow 0.38.0, Windows
Error text in a comment. I would understand if Object.keys does not work in general, but it does (2nd example, bottom), and the function returns a fixed object so not really a difference to specifying that object, but one works the other doesn't. In addition, the return type of the function is set to just that type.
Background: The more generic {[string]: string} object type works, but I tried to restrict it to the four fixed property names.
// @flow
'use strict';
type LinkedObjectsTreeObjT = {|
refs: Array<string>,
objs: Array<string>,
clobs: Array<string>,
blobs: Array<string>
|};
// Or just function (): Object {
const findLinks = function (obj: Object): { [$Keys<LinkedObjectsTreeObjT>]: string[] } {
return {
refs: [],
objs: [],
clobs: [],
blobs: []
};
};
// DOES NOT WORK:
// test.js:29
// 26: Object.keys(findLinks({})).forEach((key: $Keys<LinkedObjectsTreeObjT>) => {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ some string with unknown value. Property not found in
// 26: Object.keys(findLinks({})).forEach((key: $Keys<LinkedObjectsTreeObjT>) => {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ object type
//
// Found 1 error
Object.keys(findLinks({})).forEach((key: $Keys<LinkedObjectsTreeObjT>) => {
// ...
});
// WORKS:
Object.keys({
refs: [],
objs: [],
clobs: [],
blobs: []
}).forEach((key: $Keys<LinkedObjectsTreeObjT>) => {
// ...
});
This actually looks like a more specific bug with objects created with indexer properties. Even if B is a subtype of A, { [B]: number } is not a ~~subtype~~ supertype of { [A]: number }
type SomeStrings = 'a' | 'b' | 'c';
// FAILS: objects with *string union* keys are not supertypes of objects with *string* keys
function testStringUnionInKeys(strObject: { [SomeStrings]: number }) {
(strObject: { [string]: number })
}
// 27: (({}: { [SomeStrings]: number }): { [string]: number })
// ^ string. This type is incompatible with
// 27: (({}: { [SomeStrings]: number }): { [string]: number })
// ^ string enum
$Keys returns a string union, but even though a string union is a subtype of string, the resulting object is not.
If I remember correctly, the reason these key-related types don't work with methods like Object.keys is that Flow currently doesn't track which properties are own properties on the object and which come from a prototype. Flow accepts the type as long as the object has that property even on the prototype, whereas many key related methods operate only on own keys, so Flow is conservative and assumes it doesn't know what keys might show up.
(Also, a note that B needs to be a supertype of A for { [B]: string } to be a subtype of { [A]: string } because its in a contravariant position.)
To confirm, is this a bug or intended functionality?
UPDATE:
Flow Try link of my original example code
I just tested it, the last version where this error appears is 0.59. There is no error when you select Flow 0.60 and above, so I'm closing this issue.