hildjj / node-cbor

Encode and decode CBOR documents, with both easy mode, streaming mode, and SAX-style evented mode.
MIT License
357 stars 73 forks source link

TypeScript compiler throws errors in cbor.d.ts #127

Closed klanchman closed 3 years ago

klanchman commented 3 years ago

I was trying to upgrade from cbor v5 to v6, but ran into compiler errors from types/lib/cbor.d.ts on my TypeScript project.

A quick reproduction looks like this:

I'd expect this to compile, but instead TypeScript outputs the following errors (collapsed due to size):

TypeScript output ``` ➤ npx tsc index.ts node_modules/cbor/types/lib/cbor.d.ts:16:10 - error TS2304: Cannot find name 'comment'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:10 - error TS2552: Cannot find name 'comment'. Did you mean 'Commented'? 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:19 - error TS2304: Cannot find name 'decodeAll'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:30 - error TS2304: Cannot find name 'decodeFirst'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:43 - error TS2304: Cannot find name 'decodeAllSync'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:58 - error TS2304: Cannot find name 'decodeFirstSync'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:75 - error TS2304: Cannot find name 'diagnose'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:85 - error TS2304: Cannot find name 'encode'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:93 - error TS2304: Cannot find name 'encodeCanonical'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:110 - error TS2304: Cannot find name 'encodeOne'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:121 - error TS2304: Cannot find name 'encodeAsync'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~~~ node_modules/cbor/types/lib/cbor.d.ts:16:134 - error TS2304: Cannot find name 'decodeFirstSync'. 16 export { comment, decodeAll, decodeFirst, decodeAllSync, decodeFirstSync, diagnose, encode, encodeCanonical, encodeOne, encodeAsync, decodeFirstSync as decode }; ~~~~~~~~~~~~~~~ node_modules/cbor/types/lib/commented.d.ts:99:25 - error TS2306: File '/node_modules/@types/node/stream.d.ts' is not a module. 99 import stream = require("node/stream"); ~~~~~~~~~~~~~ node_modules/cbor/types/lib/decoder.d.ts:142:25 - error TS2306: File '/node_modules/@types/node/stream.d.ts' is not a module. 142 import stream = require("node/stream"); ~~~~~~~~~~~~~ node_modules/cbor/types/lib/diagnose.d.ts:48:25 - error TS2306: File '/node_modules/@types/node/stream.d.ts' is not a module. 48 import stream = require("node/stream"); ~~~~~~~~~~~~~ node_modules/cbor/types/lib/encoder.d.ts:224:25 - error TS2306: File '/node_modules/@types/node/stream.d.ts' is not a module. 224 import stream = require("node/stream"); ~~~~~~~~~~~~~ node_modules/cbor/types/lib/map.d.ts:30:28 - error TS2314: Generic type 'Iterable' requires 1 type argument(s). 30 constructor(iterable?: Iterable); ~~~~~~~~~~~~~~~~~~ node_modules/cbor/types/vendor/binary-parse-stream/index.d.ts:26:25 - error TS2306: File '/node_modules/@types/node/stream.d.ts' is not a module. 26 import Stream = require("node/stream"); ~~~~~~~~~~~~~ Found 18 errors. ```

I can use the skipLibCheck option to avoid the errors, but cbor itself still wouldn't have type information when I went to use it in my code.

hildjj commented 3 years ago

The library isn't set up to be imported as a module yet, unless it happens to work by accident. However, I get the same error with an index.js and checkJs=true in my tscconfig.json. The only idea I currently have is to remove the type information, any other suggestions?

hildjj commented 3 years ago

Also, I apologize for not having tested this; I assumed it would "just work". :(

hildjj commented 3 years ago

I was able to hand-edit the .ts files to get them to all work patch incoming.

hildjj commented 3 years ago

@klanchman can you do me a favor, to ensure I got this correct, please?

npm i https://github.com/hildjj/node-cbor.git

then try poking around in typescript land. I'll do a release as soon as I have confirmation from you that this works, then deal with my tooling issues later.

klanchman commented 3 years ago

Haha you beat me to it! 😄

I hadn't used TypeScript's JSDoc declaration stuff before, so this was new to me. Hand-editing was what I was starting to do too.

I did at least find that some of the errors appear to be a TypeScript problem: https://github.com/microsoft/TypeScript/issues/41800 - looks like v4.2 will have that fix. Even with that fix, it left behind the majority of the errors though. Seemed like basically the last export {...} in cbor.d.ts was the main issue, which was what I was starting to tweak by hand.


It looks like the update is mostly working: it resolved the compile issues, but decodeFirst is missing 😅 (I glanced over the old export statement and it looks like it's just decodeFirst that's gone)

hildjj commented 3 years ago

Great catch on decodeFirst. I used the copy/paste anti-pattern to create that bug. If you update it should be fixed.

klanchman commented 3 years ago

Grabbed the latest update and it looks good to me now 👍

Thank you for the blazing fast response time on this!

hildjj commented 3 years ago

6.0.1 published. cbor-cli to follow as soon as i fix whatever i broke there.