Reputation: 1996
I'm trying to create type definition in Flow for one of my objects. I'm not sure whether Flow support Symbols
, but in such cases how could I skip this warning?
const version: Version = this.getVersion(12345);
const upLink: string = version[Symbol.for('__my_amazing_symbol')];
But on validating the line above, I have the following error on run flow check
.
Error: src/local.js:197
197: const upLink: string = version[Symbol.for('__my_amazing_symbol')];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ access of computed property/element. Computed property cannot be accessed with
197: const upLink: string = version[Symbol.for('__my_amazing_symbol')];
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Symbol
Found 2 errors
This is my type of such object.
declare export type Version = {
name: string;
version: string;
};
Flow config
[ignore]
.*/node_modules/.*
lib/.*
[include]
[libs]
node_modules/@mod/types/lib/
[lints]
[options]
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
unsafe.enable_getters_and_setters=true
[version]
^0.52.0
Upvotes: 1
Views: 277
Reputation: 2287
I don't think the type error has anything to do with Symbol
per-se. What Flow is telling you is that you declared the Version
type with keys name
and version
, but now you're looking up a Symbol on that object, which isn't part of the type. You'd get a similar error if you did version["randomString"]
, because that property isn't declared to be part of the type of Version
.
As another commented has mentioned, you can ignore the error with $FlowFixMe
. But this code itself might be unsafe, since you're relying on extra symbols being defined on this object but not letting Flow prove it. You could try to model your Version
type in a way that avoids having to do these dynamic lookups.
(Edit: ah, ok, looks like I spoke too soon. Flow doesn't like using computer symbol keys, even though it in theory could check them. See this issue: https://github.com/facebook/flow/issues/2928)
Upvotes: 1