kriszyp / cbor-x

Ultra-fast CBOR encoder/decoder with extensions for records and structural cloning
MIT License
264 stars 33 forks source link

TypeScript can't find types when importing `cbor-x/index-no-eval` #100

Closed MasterKale closed 6 months ago

MasterKale commented 7 months ago

Hello @kriszyp and thank you for your excellent work with cbor-x! I come with a bit of an odd problem: TypeScript doesn't understand where to find types when importing things from cbor-x/index-no-eval:

cborx-test.ts

import { Encoder } from 'cbor-x/index-no-eval';

const encoder = new Encoder();

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2018",
    "lib": ["ES2018", "DOM"],
    "module": "commonjs",
    "esModuleInterop": true,
    "declaration": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "outDir": "dist"
  },
  "exclude": [
    "node_modules",
    "**/dist/*"
  ]
}
# TypeScript 4.6.3
$> npx tsc cborx-test.ts
cborx-test.ts:1:25 - error TS2307: Cannot find module 'cbor-x/index-no-eval'
or its corresponding type declarations.

1 import { Encoder } from 'cbor-x/index-no-eval';
                          ~~~~~~~~~~~~~~~~~~~~~~

I found out that if I duplicate index.d.ts as index-no-eval.d.ts in this project (within my local node_modules/) then subsequent runs of tsc succeed.

I wanted to PR something but after realizing that index.d.ts is manually maintained over here I couldn't think of anything clever to propose as a solution. And I didn't want to propose anything that involved maintaining a separate copy of index.d.ts as index-no-eval.d.ts as that's proposing too much maintenance and potential for type desync causing more headaches later.

Thanks for considering this.

danniss10 commented 7 months ago

I am also experience this issue when using https://github.com/MasterKale/SimpleWebAuthn which depends on this library. Any ideas on how to fix would be much appreciated šŸ™šŸ»