Skip to content

Commit 64b2cb4

Browse files
tlancinaimhoffd
authored andcommitted
fix Yauzl typings (#12)
Both `open` and `openReadStream` have overloads, so `promisify` can't know which one to choose and chooses the last one (which doesn't include options). Additionally, once we specify those two overloads, optional args become `arg | undefined` which is subtly different than not providing an arg (which is what we want to be able to do), so we specify onEntry's `openReadStream` callback explicitly as well (to have options be optional). I think this last point is also why we can't just update `yauzl`'s types to have both `options` and `callback` be optional, because `options` when `promisified` will be unioned with `undefined`, not optional (meaning you can't just leave it off, you'd have to actually pass `undefined` as the value if you want to use it). microsoft/TypeScript#13570 microsoft/TypeScript#13195
1 parent 03e8121 commit 64b2cb4

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.swp
12
dist
23
node_modules
34
.DS_Store
5+
.vscode

src/utils/unzip.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Readable } from 'stream';
22
import { promisify } from 'util';
3-
import { Entry, Options, ZipFile } from 'yauzl';
3+
import { Entry, Options, ZipFile, ZipFileOptions } from 'yauzl';
44

5-
// Override so promisify typing correctly infers params
6-
export type YauzlOpenReadStream = (entry: Entry, callback?: (err: Error, stream: Readable) => void) => Promise<Readable>;
7-
type YauzlOpen = (path: string, options: Options, callback?: (err: Error, zipfile: ZipFile) => void) => void;
8-
type UnzipOnEntry = (entry: Entry, zipfile: ZipFile, openReadStream: YauzlOpenReadStream) => void;
5+
// Specify which of possible overloads is being promisified
6+
type YauzlOpenReadStream = (entry: Entry, options?: ZipFileOptions, callback?: (err: Error, stream: Readable) => void) => void;
7+
type YauzlOpen = (path: string, options?: Options, callback?: (err: Error, zipfile: ZipFile) => void) => void;
8+
type UnzipOnEntry = (entry: Entry, zipfile: ZipFile, openReadStream: (arg1: Entry, arg2?: ZipFileOptions) => Promise<Readable>) => void;
99

1010
export async function unzip(srcPath: string, onEntry: UnzipOnEntry) {
1111
const yauzl = await import('yauzl');

0 commit comments

Comments
 (0)