gl-matrix
gl-matrix copied to clipboard
esm typings are weak or I have done something wrong
TLDR
Types from npm run build-dts
and esm build from npm run build-esm
don't work together
Problem
Plain project, no bundlers, just Typescript 3.9.6 with "es6" target to compile.
I was going to use gl-matrix as a bundled dependency because of no bundler. I got esm dist from npm run build-esm
then copy to my ./src/esm
dir. Works just fine, but no types (like mat4.create()
returns any
instead of mat4
so I can't handle it with Typescript)
So I have made index.d.ts
from npm run build-dts
. Keeping this in root folder provides no types, for this reason I just copied this right to ./src/esm
. But now I got
src/Cube.ts:7:22 - error TS2306: File '/mnt/c/Project/subtract-n-triangulate/src/esm/index.d.ts' is not a module.
7 import { mat4 } from "./esm/index.js";
~~~~~~~~~~~~~~~~
Found 1 error.
For sure I can not an expert in typescript module declarations, but something looks wrong with all this. Would be happy to get some advice or solution. Thank you.
Ok, got it.
First of all we declare our wrap module but never export it from index.d.ts
:
https://github.com/toji/gl-matrix/blob/fccd5f707432ec5cc1774575be84c9f901a198d0/utils/bundle-dts.js#L37-L38
https://github.com/toji/gl-matrix/blob/fccd5f707432ec5cc1774575be84c9f901a198d0/utils/bundle-dts.js#L10
Second, even if we replace declare
with export
we still don't have types, because of module "gl-matrix"
wrap. There is probably a reason for this to be here, but if I remove it, everything works just as I initially expect.