如何抑制流符号错误? [英] How do I suppress flow symbol errors?
问题描述
我正在尝试在Flow中为我的一个对象创建类型定义.我不确定Flow是否支持Symbols
,但是在这种情况下如何跳过此警告?
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')];
但是在验证上面的行时,在运行flow check
时出现以下错误.
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;
};
流配置
[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
推荐答案
我认为类型错误与Symbol
本身无关. Flow告诉您的是,您已使用键name
和version
声明了Version
类型,但是现在您正在该对象上查找一个Symbol,它不是该类型的一部分.如果您执行version["randomString"]
,也会收到类似的错误,因为该属性未声明为Version
类型的一部分.
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
.
正如另一条评论所述,您可以使用$FlowFixMe
忽略该错误.但是,这段代码本身可能并不安全,因为您依赖于在此对象上定义的其他符号,而不是让Flow证明它.您可以尝试为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.
(啊,好吧,看来我讲得太早了.Flow不喜欢使用计算机符号键,尽管理论上它可以检查它们.请参阅此问题:
( 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)
这篇关于如何抑制流符号错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!