SheetJS / sheetjs

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
https://sheetjs.com/
Apache License 2.0
35.2k stars 7.99k forks source link

Add `types` to subpath exports in `package.json` #2793

Closed phaseOne closed 2 years ago

phaseOne commented 2 years ago

When a project's tsconfig.json is configured to use ECMAScript modules and has strict or noImplicitAny set to true, TypeScript complains about being unable to find xlsx's declaration file:

TSError: ⨯ Unable to compile TypeScript:
src/index.ts:1:23 - error TS7016: Could not find a declaration file for module 'xlsx'. '<OMITTED>/node_modules/xlsx/xlsx.mjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/xlsx` if it exists or add a new declaration (.d.ts) file containing `declare module 'xlsx';`

1 import * as XLSX from 'xlsx';

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2015",
    "module": "Node16",
    "outDir": "./build",
    "sourceMap": true,
    "allowJs": true,
    "strict": true
  },
  "include": ["./src/**/*"]
}

The change in this PR should resolve the issue.

SheetJSDev commented 2 years ago

xlsx.zahl exports a default string while the cpexcel scripts export the same shape as the main codepage library https://github.com/SheetJS/js-codepage/blob/master/types/index.d.ts . For the purposes of this patch, just include the types for the root export and xlsx.mjs.

SheetJSDev commented 2 years ago

4dd092a cherry-picks the first two changes.

Is there a simple project template to verify correctness?

SheetJSDev commented 2 years ago

Tested in a simple svelte+TS app and the types seem to work using the changes from 1491302.