herumi / mcl-wasm

59 stars 18 forks source link

Typescript types #19

Closed jacque006 closed 3 years ago

jacque006 commented 3 years ago

Would be nice to have Typescript typing for this library, either in this repo or via DefinetlyTyped (@types/mcl-wasm).

herumi commented 3 years ago

Okay, I'll make it. Please wait a moment.

herumi commented 3 years ago

I'm developing https://github.com/herumi/mcl-wasm/blob/typescript/src/index.d.ts and it seems to run mostly well. But it has something wrong. If I comment out mapToG1,

% npx tsc test/test-ts.ts
test/test-ts.ts:247:13 - error TS2345: Argument of type 'Fr' is not assignable to parameter of type 'Fp'.
  Property 'mapToG1' is missing in type 'IntType' but required in type 'Fp'.

247     z.set_a(c)
                ~

  src/index.d.ts:49:5
    49     mapToG1(): G1; // QQQ : why does this cause an error?
           ~~~~~~~
    'mapToG1' is declared here.

test/test-ts.ts:447:25 - error TS2345: Argument of type 'Fr' is not assignable to parameter of type 'G1'.
  Type 'IntType' is missing the following properties from type 'G1': setX, setY, setZ, getX, and 5 more.

447   const e = mcl.pairing(mcl.mul(mpk, r), Q)

I I wonder why mcl.add for Fp returns mcl.Fr. I'm just starting to study TypeScript and I'm not sure.

herumi commented 3 years ago

I've solved it by adding a never member to distinguish Fp and Fr.

herumi commented 3 years ago

https://github.com/herumi/mcl-wasm/commit/6c4d1c02afc15e6df2c2cfaf41b5b2b1b24fa14c

jacque006 commented 3 years ago

Thanks @herumi !