SVG types support in Flow.js?

别来无恙 提交于 2021-02-05 10:42:06

问题


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:


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.



来源:https://stackoverflow.com/questions/59578362/svg-types-support-in-flow-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!