vosk-api icon indicating copy to clipboard operation
vosk-api copied to clipboard

Refactored nodejs bindings to use typescript

Open mmende opened this issue 3 years ago • 7 comments

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.

mmende avatar May 21 '21 15:05 mmende

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?

nshmyrev avatar May 26 '21 10:05 nshmyrev

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.

mmende avatar May 26 '21 10:05 mmende

Added getModelAsync and getSpkModelAsync which allow optionally loading models in a different thread

mmende avatar Jul 21 '21 10:07 mmende

@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 avatar Nov 15 '21 14:11 alangibson

@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).

mmende avatar Dec 08 '21 08:12 mmende

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

MartinMuzatko avatar Dec 18 '21 22:12 MartinMuzatko

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, [string | null]>; vosk_model_free: ForeignFunction<void, [Pointer]>; ... 17 more ...; vosk_gpu_thread_init: ForeignFunction<...>; }' is not assignable to type 'VoskLibrary'. The types of 'vosk_model_new.async' are incompatible between these types. Type '(args_0: string | null, args_1: (err: any, value: Pointer) => void) => void' is not assignable to type '(modelPath: string, cb: (err: Error | null, model: Type<Pointer> | null) => void) => void'. Types of parameters 'args_1' and 'cb' are incompatible. Types of parameters 'model' and 'value' are incompatible. Type 'Pointer' is missing the following properties from type 'Type<Pointer>': size, indirection, get

307 const libvosk: VoskLibrary = ffi.Library(soname, {

donyam avatar Aug 06 '22 07:08 donyam