NorthwoodsSoftware/GoJS

`TextBlock().bind` missing type

renancouto opened this issue · 2 comments

When using GoJS with Typescript the types provided for TextBlock().bind seem to be incomplete on the 2nd argument that is defined to accept only a string, while it also works with functions, like this:

new go.TextBlock().bind("text", (modelObj) => modelObj.prop)

This throws an error on the 2nd argument with the following message:

Argument of type '(modelObj: any) => any' is not assignable to parameter of type 'string'.ts(2345)

Would be nice to be able to use functions and also to have the model's object properties infered if possible.

The problem with that rests on determining what conditions would re-evaluate such a binding. The string is not just to define a source on the modelObj data, but to name the (typically, sole) property that needs to be observed for changes as well.

Normally writing:

new go.TextBlock().bind("text", "prop")

Will re-evaluate when "prop" is set (via model.set), and only re-evaluate then. And if you need a more complex binding than just observing "prop", you would write:

// An empty-string binding with a conversion function
new go.TextBlock().bind("text", "", (modelObj) => modelObj.prop)

Empty-string bindings are re-evaluated when any data on the model object changes, which are by this nature less efficient, so they are not something you'd want to use unless you had to.

I hope that makes sense. Is there some use case not covered here that I am missing?

Hey, thanks a lot for the explanation! The empty string works fine.