简体   繁体   English

如何在 typescript 中为 Model 定义 nodeCategoryProperty function?

[英]How to define nodeCategoryProperty function for a Model in typescript?

nodeCategoryProperty function expects to have the signature - (a: ObjectData, b?: string) => string , which, IMO, should be (a: ObjectData, b?: string) => string | void nodeCategoryProperty function 期望具有签名 - (a: ObjectData, b?: string) => string ,IMO 应该是(a: ObjectData, b?: string) => string | void (a: ObjectData, b?: string) => string | void , as this function should not return anything if it is being used as a setter. (a: ObjectData, b?: string) => string | void ,因为这个 function 不应该返回任何东西,如果它被用作设置器。

If the second argument is supplied, the function should modify the node data object so that it has that new category name.如果提供了第二个参数,则 function 应修改节点数据 object 使其具有新的类别名称。

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

For the function I have defined -对于 function 我已经定义了 -

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

I am getting TypeScript error:我收到 TypeScript 错误:

TS2366: Function lacks ending return statement and return type does not include 'undefined'. TS2366:Function 缺少结束返回语句并且返回类型不包括“未定义”。

Here is an example of storing the category name as a property of the "d" property on the data object:以下是将类别名称存储为数据 object 上“d”属性的属性的示例:

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

Note the use of function instead of using an arrow function, so that one can check the number of arguments .注意使用function而不是使用箭头 function,以便可以检查arguments的数量。

The same kind of implementation can be used for the other "...Property" functional properties of the Model classes.相同类型的实现可用于Model类的其他“...Property”功能属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在打字稿中定义带有“this”上下文的函数 - How to define a function with "this" context in typescript 如何使用Typescript定义一个通用的function签名 - How to use Typescript to define a generic function signature 如何在 typescript 中键入定义 zip function - how to type define a zip function in typescript 如何在angular2 [typescript]中定义具有复杂json结构的模型? - How to define model with complex json structure in angular2 [typescript]? 如何用打字稿在猫鼬模型中定义自定义查询助手? - How to define custom query helper in mongoose model with typescript? 如何通过在打字稿中调用函数来关闭Bootstrap 4模型 - How to close bootstrap 4 model by calling a function in typescript Typescript:如何用布尔值或回调函数定义联合类型? - Typescript: how do you define a union type with boolean or a callback function? TypeScript-如何使用任何类型的参数定义函数类型 - TypeScript - How to define Function Type having arguments with any Type 如何在 React TypeScript 中定义一次类型并将其分配给函数和组件? - How to define a type once and assign it to a function and a component in React TypeScript? 如何根据Typescript中的字符串参数定义函数的参数类型? - How to define a function's argument type dependent on string argument in Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM