Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript does not find jszip types

I have a strange issue where typescript does not find the types for jszip.

  • other types work fine
  • the types seem to be in the correct location

in my source-file I use these imports e.g.

import * as fast_csv from 'fast-csv';
import { Response } from 'express';
import fs from 'fs';
import * as JSZip from 'jszip';

all work fine, except for jszip: es

Error:(9, 24) TS7016: Could not find a declaration file for module 'jszip'. 'ROOT/node_modules/jszip/dist/jszip.min.js' implicitly has an 'any' type.

When I look into my (one and only) node_modules/@types dir, all files are there: enter image description here

here are the relevant entries from package.json

  "dependencies": {
    "@hapi/joi": "^15.0.1",
    "fast-csv": "^2.5.0",
    "jszip": "^3.2.1"
  },
  "devDependencies": {
    "@types/express": "^4.16.0",
    "@types/hapi__joi": "^15.0.1",
    "@types/jszip": "^3.1.6",
   }

notes:

  • I thought typescript only uses the name: e.g. when importing jszip it should search for @types/jszip
    • but it seems that I am wrong, because the types of @hapi/joi are called @types/hapi__joi and they work fine
  • I think also the version mismatch shoudl not be an issue: "jszip": "^3.2.1" vs. "@types/jszip": "^3.1.6", right?
  • the code-completion in the IDE shows also the types: enter image description here

Any idea, what I am missing?

related links:

  • typescript doc: consume declaration files
  • @types, typeRoots and types
  • npm @types/jszip
like image 820
TmTron Avatar asked Dec 07 '25 10:12

TmTron


1 Answers

Update 04.2020

It seems that the issue is fixed in jszip 3.3.0: see Fix browser module resolution #614

Moreover I think the issue was only related to the client side (angular) build. Since node has its own stream package

Original

I found the issue: in tsconfig.json we had an explicit path definition:

"paths": {
  "jszip": ["node_modules/jszip/dist/jszip.min.js"],

after removing this, the build failed with:

ERROR in C:/devroot/node_modules/jszip/lib/readable-stream-browser.js
Module not found: Error: Can't resolve 'stream' in 'C:\devroot\node_modules\jszip\lib'

and the solution to this is to install the stream package (thanks to this issue-comment)

npm i stream

or when using typedi:

typedi stream

and now the build works fine.

Note: maybe there is a better way to fix this - see this issue-comment

like image 80
TmTron Avatar answered Dec 10 '25 09:12

TmTron