Skip to content

Commit a064ecf

Browse files
authored
Replace get<Raw>(...) with an overload (#805)
One overload with `options?: { raw?: false }` and one with `options: { raw: true }` allows being specific about the return type for each. So, the behavior is unchanged but it becomes easier for external libraries to write an interface with a correct *partial* implementation. My use-case: I'm working on a library that expects a keyv-like object passed in. The user is allowed to pass in a different implementation, as long as the basic `get`, `set` and `delete` exist. But they don't need to provide an implementation for the `{ raw: true }` variant. So, I'm trying to write an interface that corresponds to that, which `new Keyv<string>(...)` conforms to. This is hard/impossible to do without some `as any` hacks right now, but the change in this commit makes it possible. Signed-off-by: Misha Kaletsky <[email protected]>
1 parent 813df98 commit a064ecf

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

packages/keyv/src/index.d.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,14 @@ declare class Keyv<Value = any, Options extends Record<string, any> = Record<str
2727
constructor(uri?: string, options?: Keyv.Options<Value> & Options);
2828

2929
/** Returns the value. */
30-
get<Raw extends boolean = false>(key: string, options?: {raw?: Raw}):
31-
Promise<(Raw extends false
32-
? Value
33-
: Keyv.DeserializedData<Value>) | undefined>;
30+
get(key: string, options?: {raw?: false}): Promise<Value | undefined>;
31+
/** Returns the raw value. */
32+
get(key: string, options: {raw: true}): Promise<Keyv.DeserializedData<Value> | undefined>;
3433

3534
/** Returns an array of values. Uses `store.getMany` if it exists, otherwise uses parallel calls to `store.get`. */
36-
get<Raw extends boolean = false>(
37-
key: string[],
38-
options?: {raw?: Raw}
39-
): Promise<
40-
Array<(Raw extends false ? Value : Keyv.DeserializedData<Value>) | undefined>
41-
>;
35+
get(key: string[], options?: {raw?: false}): Promise<Array<Value | undefined>>;
36+
/** Returns an array of raw values. Uses `store.getMany` if it exists, otherwise uses parallel calls to `store.get`. */
37+
get(key: string[], options: {raw: true}): Promise<Array<Keyv.DeserializedData<Value> | undefined>>;
4238

4339
/**
4440
* Set a value.

0 commit comments

Comments
 (0)