vosk-api
vosk-api copied to clipboard
Refactored nodejs bindings to use typescript
This PR contains a refactored version of the nodejs bindings using typescript. This enables autocompletion and static code analysis when including the library in typescript or javascript projects. JSDocs is a nice start, but when including the library within a typescript project, typescript will complain about a missing declaration file. Furthermore, as code is written in typescript itself, the declaration files doesn't have to be updated manually when working on the library.
I also added prettier and eslint for a consistent code style within the nodejs library and refactored the demo scripts so that the dependencies of the demos must not be included in the library itself.
The PR also makes it a little bit easier to work on the nodejs library by including a script that downloads the shared libraries for development and provides a watch server that reruns a test script once the code changes somehow.
Hi @mmende
I'm sorry, I'm not very deep in all those JS things, does this change make things incompatible with plain JS? Should we just have 2 parallel version instead?
Hi @nshmyrev
No, this doesn't change anything when using the library with plain JS. The code is transpiled to pure JS and separate type declaration files (.d.ts
) in the build step. You can see the output in the dist
folder when running yarn run build
. A parallel typescript version would also be ok in my opinion even though this would add some redundancy.
Added getModelAsync and getSpkModelAsync which allow optionally loading models in a different thread
@mmende Have you considered submitting this to DefinitelyTyped? This would be awesome to have, but there doesn't appear to be much appetite to merge it here.
@alangibson Yes I thought about it. The problem with separate types however is, that I'd had to keep track of api changes within the vosk nodejs implementation to prevent wrong or outdated type definitions. I also thought about publishing a separate module as @nshmyrev already suggested (maybe something like vosk-ts
).
I'd love to see the library to have official type support. If it doesn't get merged, It would be great to have the types seperately or the fork. While the fork is probably easier to maintain
node_modules\ts-node\src\index.ts:859
return new TSError(diagnosticText, diagnosticCodes, diagnostics);
^
TSError: ⨯ Unable to compile TypeScript:
src/lib.ts:307:7 - error TS2322: Type '{ vosk_model_new: ForeignFunction<Pointer
307 const libvosk: VoskLibrary = ffi.Library(soname, {