【问题标题】:How to define nodeCategoryProperty function for a Model in typescript?如何在打字稿中为模型定义 nodeCategoryProperty 函数?
【发布时间】:2021-12-31 05:27:36
【问题描述】:

nodeCategoryProperty 函数需要签名 - (a: ObjectData, b?: string) => string,IMO 应该是 (a: ObjectData, b?: string) => string | void,因为如果这个函数被用作 setter,它不应该返回任何东西。

如果提供了第二个参数,函数应该修改节点数据 对象,使其具有新的类别名称。

https://gojs.net/latest/api/symbols/Model.html#nodeCategoryProperty

对于我定义的函数-

    const categoryPropertyFunction = (partData: ObjectData, category?: string): string => 
    {
      if (category) partData.type = category;
      else return partData.type;
    };

我收到 TypeScript 错误:

TS2366:函数缺少结束返回语句并且返回类型不包括“未定义”。

【问题讨论】:

  • "IMO, 应该是 (a: ObjectData, b?: string) => string | void" 但显然不是,因为你使用你的假设。为什么不符合 1. 文档给你的接口 2. 似乎正确匹配你不使用时得到的错误?

标签: javascript typescript gojs


【解决方案1】:

以下是将类别名称存储为数据对象上“d”属性的属性的示例:

  const model = . . .;
  model.nodeCategoryProperty = function (obj: go.ObjectData, str?: string): string {
    if (arguments.length > 1) obj.d.category = str;
    return obj.d.category;
  };

注意使用function而不是使用箭头函数,这样可以查看arguments的数量。

Model 类的其他“...Property”功能属性可以使用相同类型的实现。

【讨论】:

    猜你喜欢
    • 2019-12-25
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2017-04-13
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多