Skip to content

Commit 05b0f55

Browse files
authored
Clean up type casts by passing a generic (#7)
There are a lot of unnecessary type casts because of a lack of generics. This should clean up a few of those.
1 parent 98f1990 commit 05b0f55

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

packages/memcache-client/src/lib/client.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import assert from "assert";
2-
import { optionalRequire } from "optional-require";
32
import { Socket } from "net";
43

5-
const Zstd = optionalRequire("zstd.ts");
4+
import * as Zstd from "zstd.ts";
65

76
import nodeify from "./nodeify";
87
import ValuePacker from "./value-packer";
@@ -226,19 +225,19 @@ export class MemcacheClient extends EventEmitter {
226225
options = {};
227226
}
228227

229-
return this._callbackSend(data, key, options, callback) as unknown as Promise<ValueType>;
228+
return this._callbackSend(data, key, options, callback);
230229
}
231230

232231
// the promise only version of send
233-
xsend(
232+
xsend<ValueType>(
234233
data: StoreParams | SocketCallback,
235234
key: string,
236235
options?: StoreCommandOptions
237-
): Promise<unknown> {
236+
): Promise<ValueType> {
238237
return this._servers.doCmd(
239238
(c: MemcacheConnection) => this._send(c, data, options || {}),
240239
key
241-
) as Promise<unknown>;
240+
) as Promise<ValueType>;
242241
}
243242

244243
// a convenient method to send a single line as a command to the server
@@ -249,7 +248,7 @@ export class MemcacheClient extends EventEmitter {
249248
options?: CommonCommandOption,
250249
callback?: ErrorFirstCallback
251250
): Promise<Response> {
252-
return this.send(
251+
return this.send<Response>(
253252
(socket) => {
254253
let line = data;
255254
if (options?.noreply) {
@@ -260,7 +259,7 @@ export class MemcacheClient extends EventEmitter {
260259
key || "",
261260
options,
262261
callback
263-
) as unknown as Promise<Response>;
262+
);
264263
}
265264

266265
// "set" means "store this data".
@@ -346,7 +345,7 @@ export class MemcacheClient extends EventEmitter {
346345
key,
347346
options as CommonCommandOption,
348347
callback
349-
) as unknown as Promise<string[]>;
348+
);
350349
}
351350

352351
// incr key by value, fire & forget with options.noreply
@@ -361,7 +360,7 @@ export class MemcacheClient extends EventEmitter {
361360
key,
362361
options as StoreCommandOptions,
363362
callback
364-
) as unknown as Promise<string>;
363+
);
365364
}
366365

367366
// decrease key by value, fire & forget with options.noreply
@@ -376,7 +375,7 @@ export class MemcacheClient extends EventEmitter {
376375
key,
377376
options as StoreCommandOptions,
378377
callback
379-
) as unknown as Promise<string>;
378+
);
380379
}
381380

382381
// touch key with exp time, fire & forget with options.noreply
@@ -391,12 +390,12 @@ export class MemcacheClient extends EventEmitter {
391390
key,
392391
options as CommonCommandOption,
393392
callback
394-
) as unknown as Promise<string[]>;
393+
);
395394
}
396395

397396
// get version of server
398397
version(callback?: OperationCallback<Error, string[]>): Promise<string[]> {
399-
return this.cmd(`version`, "", {}, callback) as unknown as Promise<string[]>;
398+
return this.cmd(`version`, "", {}, callback);
400399
}
401400

402401
// flush all keys from the server, optionally after a delay in seconds
@@ -672,6 +671,7 @@ export class MemcacheClient extends EventEmitter {
672671
serverKey,
673672
keys: serverKeys,
674673
});
674+
return undefined;
675675
}
676676
})
677677
);
@@ -731,13 +731,13 @@ export class MemcacheClient extends EventEmitter {
731731
}
732732

733733
// internal send that expects all params passed (even if they are undefined)
734-
_callbackSend(
734+
_callbackSend<ValueType>(
735735
data: StoreParams | SocketCallback,
736736
key: string,
737737
options?: Partial<CasCommandOptions>,
738738
callback?: ErrorFirstCallback
739-
): Promise<unknown> {
740-
return nodeify(this.xsend(data, key, options), callback);
739+
): Promise<ValueType> {
740+
return nodeify<ValueType>(this.xsend(data, key, options), callback);
741741
}
742742

743743
_unpackValue(result: PackedData): number | string | Record<string, unknown> | Buffer {

packages/memcache-client/src/lib/nodeify.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ export type CallbackablePromise<T extends any> = Promise<T> & {
44
nodeify?: (callback: ErrorFirstCallback) => void;
55
};
66

7-
export default function nodeify(
8-
promise: CallbackablePromise<unknown>,
7+
export default function nodeify<ValueType>(
8+
promise: CallbackablePromise<ValueType>,
99
callback?: ErrorFirstCallback
10-
): Promise<unknown> {
10+
): Promise<ValueType> {
1111
if (callback) {
1212
if (promise.nodeify !== undefined) {
1313
promise.nodeify(callback);

0 commit comments

Comments
 (0)