Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions types/keyv/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,31 @@
/// <reference types="node" />
import { EventEmitter } from 'events';

declare class Keyv<TValue = any> extends EventEmitter {
type WithRequiredProperties<T, K extends keyof T> = T & Required<Pick<T, K>>;

declare class Keyv<TValue = any, TOpts extends { [key: string]: any } = {}> extends EventEmitter {
/**
* `this.opts` is an object containing at least the properties listed
* below. However, `Keyv.Options` allows arbitrary properties as well.
* These properties can be specified as the second type parameter to `Keyv`.
*/
opts: WithRequiredProperties<
Keyv.Options<TValue>,
'deserialize' | 'namespace' | 'serialize' | 'store' | 'uri'
> &
TOpts;

/**
* @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
*/
constructor(opts?: Keyv.Options<TValue>);
constructor(opts?: Keyv.Options<TValue> & TOpts);
/**
* @param uri The connection string URI.
*
* Merged into the options object as options.uri.
* @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
*/
constructor(uri?: string, opts?: Keyv.Options<TValue>);
constructor(uri?: string, opts?: Keyv.Options<TValue> & TOpts);

/** Returns the value. */
get<TRaw extends boolean = false>(key: string, options?: { raw?: TRaw }):
Expand Down
19 changes: 19 additions & 0 deletions types/keyv/keyv-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,22 @@ new Keyv();
await keyv.delete('foo'); // $ExpectType boolean
await keyv.clear(); // $ExpectType void
})();

{
// Base options accessible and typed correctly with TOpts unused
const keyv = new Keyv<string>();
keyv.opts.namespace; // $ExpectType string
keyv.opts.deserialize; // $ExpectType (data: string) => DeserializedData<string> | undefined
keyv.opts.serialize; // $ExpectType (data: DeserializedData<string>) => string
keyv.opts.store; // $ExpectType Store<string>

// Base options accessible and typed correctly with TOpts used
const customOptsKeyv = new Keyv<string, { customProperty: string }>();
customOptsKeyv.opts.namespace; // $ExpectType string
customOptsKeyv.opts.deserialize; // $ExpectType (data: string) => DeserializedData<string> | undefined
customOptsKeyv.opts.serialize; // $ExpectType (data: DeserializedData<string>) => string
customOptsKeyv.opts.store; // $ExpectType Store<string>

// TOpts typings are correct when used
customOptsKeyv.opts.customProperty; // $ExpectType string
}