-
Is there any example / doc that shows typical usage of the JSDoc features supported by Stitch? However currently I'm trying to find a way to document the fields of a struct but am unable to find how ( / if?) it can be done. Eg. in situations:
In fact, for the above, is it possible to specify the key type as well? Made some attempts but neither seem to work:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Stitch doesn't support Typescript-style type annotations -- it's still mostly just the kind of thing that Feather does, and uses Feather type-name conventions. Since Feather doesn't have a way to provide a struct type without actually having the struct, neither does Stitch (currently). It's something I'd like to start supporting eventually, since it's pretty cumbersome to create structs just to use as types. What we currently do is just create constructors to model types. We figure that they'll get pruned during compile since they aren't used. For example: function Config() constructor {
x0: 0; // To get the inferred type "Real"
// Or use Stitch-specific JSDoc variable-declaration comments
/// @instancevar {Real} x1
} Then you'd reference that constructor as a type elsewhere: /// @param {Struct.Config} config
function doSomething(config) {...} Stitch does try to update struct types as it sees variables get added to them, but the parser only evaluates string literals as keys.
For your case function Resource () constructor {
/// @instancevar {string} name
/// @instancevar {Asset.GMSprite} sprite
}
// Then type-annotate your literal to allow any string as a key
/// @type {Struct<Struct.Resource>}
var resources = {};
// OR, create another constructor with all of the key names specified
function Resources () constructor {
/// @instancevar {Struct.Resource} water
/// @instancevar {Struct.Resource} fire
}
/// @type {Struct.Resources}
var resources = {}; (I'm pretty sure that Stitch should give you autocomplete help in your JSDoc types, which is very helpful since Feather-style type names are verbose and hard to remember. You can also right-click a symbol to get a handful of "Copy as JSDoc" options, which I think are also in the command palette. |
Beta Was this translation helpful? Give feedback.
Awesome, this solution works! My above examples were written in Obj:Create (both definition and usage). I'd have thought that especially as long as they're in the same location they'd be able to find eachother. But when moving it into a gml script it does seem to work as expected.
In fact, it seems only the type definition needs to be moved to the script, then the usage can refer to it from anywhere, including inside Obj:Create (and anywhere else I'd assume).
The "Copy…