typescript - SVG.js:检测“元素”是否为“组”
问题描述
我需要确定通过检索的元素SVG.get()
是否是组(SVG.G
)。我认为这将是一个简单的instanceof
检查,但我never
在 Typescript 中遇到类型错误。
这是代码:
// Get the element from the <defs> section
const newuse = svg.get(key);
// Set the fill colour
const fill = "red";
// If it's a group, go through and fill all children with the "data- playerfill" attribute
if ( newuse instanceof svg.G ) {
// I have to do this in Typescript because the Set doesn't have a "fill" function defined
(newuse as svg.G).select("[data-playerfill=true]").each(function(this: svg.Element) { this.fill(fill); });
} else {
// Just a single element that you fill
newuse.fill(fill);
} }
问题是我在最后一行收到以下错误:
Property 'fill' does not exist on type 'never'.
据我所知,这意味着永远不会评估 else 子句。但我不确定为什么。都是吗 ?SVG.Element
_ SVG.G
我当然不这么认为。
任何指导表示赞赏。谢谢!
解决方案
instanceof 测试现在应该在 3.0.12 中正常工作,因为 Svg.G 现在已正确声明为一个类。
推荐阅读
- javascript - 如何修改正则表达式以限制字符数最少 8 到最多 20
- java - 怎么计算!构造一个算法,允许您输入一个 4 位整数并计算第一位和最后一位数字的总和
- ruby-on-rails - 通过关系在 has_many 上的 ActiveRecord::InvalidForeignKey
- python-3.x - 如何处理带有嵌套列表和字符串元素的列表?
- javascript - 在laravel中触发事件后如何获取特定div中的值?
- python - 如何合并具有共同子字符串的字符串以在 Python 的数据框中生成一些组
- c++ - 图重路由算法黑客排名
- apache-pulsar - 用于日志记录的“旁观者”Pulsar Consumer
- python-3.x - SQLAlchemy 没有属性 Column
- xaml - 在 SkiaSharp (UWP) 中正确对齐 UI 元素