英文:
SVG types support in Flow.js?
问题
我看了各种 github thread,似乎在当前版本的 Flow.js 中还没有合并/发布 SVG 支持(我正在使用 VS Code 扩展 vscode-flow-ide
)。我正在尝试使用 SVG 引用,目前不得不使用不太具体的类型(Element
)和 // $FlowFixMe
的混合来解决这些问题 - 从更广泛的类型安全角度来看并不理想。
有人解决了这个问题吗?我目前正在使用 /flow/lib/dom.js
,但它缺少这些类型。
英文:
I've looked around at various github threads and it seems as if SVG support is not merged / shipped in current versions of Flow.js (I'm using VS Code extension vscode-flow-ide
). I'm trying to work with SVG refs and for now have had to use a mix of less-specific type (Element
) and // $FlowFixMe
to work around these - not ideal from a broader type-safety perspective.
Anyone solved this? I'm using /flow/lib/dom.js
currently, but it lacks these types.
答案1
得分: 2
中文翻译:
临时解决方案 直到官方包含...
对于缺失的类型,为您打算使用的属性子集实现您自己的类型。
例如,我正在操作SVG,所以在一个名为 SVGElement.js.flow
的文件中(以 DOM 类型 命名):
type SVGElement = HTMLElement &
{
fonts: [],
fillOpacity: number
//...
//实际上,SVGElement 有更多的属性,但您可以只包含
//您将要使用的属性,这样可以保证类型安全。
//随着时间的推移,您可以逐步添加您需要的任何内容。
}
只为方法和属性的子集添加类型比实现整个类型更容易。
&
允许从 /flow/lib/dom.js
中的 HTMLElement
进行继承;请参阅 交叉类型。
英文:
Interrim solution until the official inclusions are made...
For types that are missing, implement your own for that subset of properties that you intend to use.
For example, I am manipulating SVGs, so in a file SVGElement.js.flow
(named after the DOM type):
type SVGElement = HTMLElement &
{
fonts: [],
fillOpacity: number
//...
//In reality, SVGElement has many more properties, but you can include
//only those you will use and which thus require type-safety.
//You can add whatever you need, incrementally, as time passes.
}
Typing only subsets of methods & properties is easier than having to implement the entire type.
The &
allows inheritance from HTMLElement
present in /flow/lib/dom.js
; see intersection types.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论