You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Figma插件安装官方TS类型后仍提示SceneNode无cornerRadius属性

这个报错不属于配置遗漏问题,是TypeScript类型系统的正常校验提示:
figma.currentPage.selection返回的元素类型为SceneNode,它是所有可选中画布节点的联合类型,其中仅部分节点类型(矩形、帧、多边形等)具备cornerRadius属性,其余如文本、组、切片等节点均无该属性,因此TS不允许直接访问不确定存在的属性。

你可以通过以下几种方式解决问题,无需使用// @ts-ignore:

  • 方案1:类型收窄(最安全)
    先判断选中节点的类型,确认属于带圆角属性的节点后再访问对应属性:
    figma.showUI(__html__);
    const selectedNode = figma.currentPage.selection[0];
    // 判断节点是否为支持圆角的类型
    if (['FRAME', 'RECTANGLE', 'ELLIPSE', 'POLYGON', 'STAR'].includes(selectedNode.type)) {
      console.log(selectedNode.cornerRadius);
    }
    
  • 方案2:自定义类型守卫(复用性高)
    如果你需要在多处判断节点是否带圆角属性,可以封装通用的类型守卫函数:
    function hasCornerRadius(node: SceneNode): node is SceneNode & { cornerRadius: number } {
      return 'cornerRadius' in node;
    }
    
    const selectedNode = figma.currentPage.selection[0];
    if (hasCornerRadius(selectedNode)) {
      console.log(selectedNode.cornerRadius);
    }
    
  • 方案3:类型断言(适合确定节点类型的场景)
    如果你能100%确认当前选中的节点一定属于支持圆角的类型,可以直接使用类型断言跳过校验:
    console.log((figma.currentPage.selection[0] as RectangleNode).cornerRadius);
    

内容的提问来源于stack exchange,提问作者crowhill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 23:39:03