RusticiSoftware/TinCanJS

Typscript type definitions?

Opened this issue · 3 comments

As someone planning to use xAPI from Typescript code, it would be great if TinCanJS had javascript typings to assist with auto-complete and type-checking, but if type definitions exist I cannot find any.

The generated documentation seems like it would go a pretty long way towards this, if there was a way to automate the conversion of the annotation comments into Typescript definitions (I'm not aware of any). There are also some holes in the documentation. (e.g. a lot of places in the doc specify a callback just as type function without further specifying the type of the function, so i either need to guess or refer to the code). Typescript allows specifying the type of a function, which makes sure that the calling code is passing in the right kind of a callback.

I'm currently bridging the gap by writing some of my own type definitions just for parts of the xAPI that I need, e.g.

interface TinCanRetrieveActivityProfileConfig {
    activity: TinCanActivity;
    callback: (err,activityProfile:TinCanActivityProfile)=>void
}

interface TinCanLRS {
    retrieveActivityProfile(key:string, cfg:TinCanRetrieveActivityProfileConfig)
   ...
}

There aren't any plans to support Typescript at the moment, and I don't know of any tools as you mentioned to do so automatically. The code is fairly consistent so if a tool existed it should at least work across the majority of the code base.

Anywhere that there is a callback it will be the same execution style, I don't know what the types of functions are, but if void is what you mean, then AFAIK they should all be void.

TinCanJS is unlikely to significantly change at this point, but we may get to a version 2.x series that will be a complete overhaul of the code base to take advantage of more modern JS. I can't say whether that would include Typescript but I also wouldn't rule it out. I also can't say when that may occur, devoting resources to a non-revenue generating OSS project against a stable spec isn't generally a priority.

Thanks for the response. By "types of the functions" i meant not just the return type, but really the whole signature. For the callbacks, obviously the return type is uninteresting and it is the parameters which are more useful to know the types of.

e.g. the declaration callback: (err,activityProfile:TinCanActivityProfile)=>void tells us that the second parameter of the callback is a TinCanActivityProfile and then the type-checking and intellisense will know helpful things about the second argument when writing the callback implementation.

Since the yuidoc software that you are using supports handlebar templates, I experimented briefly with creating some templates which would output in a file that is close to Typescript declarations, though I did not get too far down that road. It seems like that would be a good way to do a first pass but it would still require quite a lot of manual work to complete it.

@brianjmiller I have a fair amount of my own Typescript definitions now that I'm using alongside this library, to help make sense of it, so if you do start down the road of 2.0 please let me know and I can share some of the type definitions I have as reference.