jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Typescript typings error caused by inclusion of NodeJS typings

Open blackshadev opened this issue 4 years ago • 8 comments

I updated to your latest version and noticed I got some typing errors. After some investigation I noticed you added typescript typings between version 3.2.x and 3.4.x. The new typing file (index.d.ts) included the following line

/// <reference types="node" />

Which includes all NodeJS typings when using jszip in typescript. Not really the desired behavior when using jszip in the browser instead of in NodeJS.

It causes errors like:

ERROR in node_modules/@types/node/index.d.ts(139,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type 'any', but here has type 'NodeRequire'.

I don't know why you include the NodeJS typings, but can you either not include it and use interfaces for the typings you need, or have 2 separate typings, one for NodeJS and one for the browser?

blackshadev avatar May 25 '20 09:05 blackshadev

Thanks for filing this issue. The reason Node typings are included are because the inputs and outputs can take Node's ReadableStream and Buffer.

I can't see a way to provide browser and Node typings without requiring configuration from the consumer. Do you know of a way?

Stuk avatar May 31 '20 18:05 Stuk

Good question, normally you would import specific types like

import { ReadableStream, Buffer } from "@types/node"

But since node uses ambient declarations I am not sure if that it at all possible. You can always copy the typings over and use them. What I did myself was just remove the typings and be fine.

Another thing you can do is don't package typings with your package and use the DefinitelyTyped project to export 2 different kind of typings, one for node and one for browsers.

blackshadev avatar Jun 01 '20 08:06 blackshadev

Another Problem: internalStream (https://stuk.github.io/jszip/documentation/api_zipobject/internal_stream.html) is missing in the index.d.ts file?

killerfurbel avatar Jun 03 '20 13:06 killerfurbel

@blackshadev just wondering, do you define the type of require yourself in your project, instead of using Node's require?

The error you posted seems to indicate that require is being defined twice.

Stuk avatar Jun 13 '20 01:06 Stuk

@killerfurbel could you file a new issue? (I also have limited time at the moment, so if you could create a PR to fix it then I'd really appreciate that!)

Stuk avatar Jun 13 '20 01:06 Stuk

@Stuk Yeah, I have my "own" require and timeout definitions because I don't use NodeJS, but the browser with webpack. The typings of webpack+browser is a bit different than NodeJS.

blackshadev avatar Jun 13 '20 07:06 blackshadev

so, is there any solution to this problem? My setTimeout definition has been overwritten with export function setTimeout<[]>( callback: () => void, ms?: number | undefined): NodeJS.Timeout

Gagydzer avatar Feb 25 '22 12:02 Gagydzer

so, is there any solution to this problem? My setTimeout definition has been overwritten with export function setTimeout<[]>( callback: () => void, ms?: number | undefined): NodeJS.Timeout

+1. Same problem here. Guess I will need to find something else for replacement.

shqyking avatar Sep 06 '22 06:09 shqyking