diff --git a/package/src/MMKV.ts b/package/src/MMKV.ts index 5ee1f01b..0b808f33 100644 --- a/package/src/MMKV.ts +++ b/package/src/MMKV.ts @@ -9,12 +9,13 @@ import type { } from './Types'; import { addMemoryWarningListener } from './MemoryWarningListener'; -const onValueChangedListeners = new Map void)[]>(); +type ListenerType = (key: string) => void; +const onValueChangedListeners = new Map(); /** * A single MMKV instance. */ -export class MMKV implements MMKVInterface { +export class MMKV implements MMKVInterface { private nativeInstance: NativeMMKV; private functionCache: Partial; private id: string; @@ -65,33 +66,33 @@ export class MMKV implements MMKVInterface { get isReadOnly(): boolean { return this.nativeInstance.isReadOnly; } - set(key: string, value: boolean | string | number | ArrayBuffer): void { + set(key: K, value: boolean | string | number | ArrayBuffer): void { const func = this.getFunctionFromCache('set'); func(key, value); this.onValuesChanged([key]); } - getBoolean(key: string): boolean | undefined { + getBoolean(key: K): boolean | undefined { const func = this.getFunctionFromCache('getBoolean'); return func(key); } - getString(key: string): string | undefined { + getString(key: K): string | undefined { const func = this.getFunctionFromCache('getString'); return func(key); } - getNumber(key: string): number | undefined { + getNumber(key: K): number | undefined { const func = this.getFunctionFromCache('getNumber'); return func(key); } - getBuffer(key: string): ArrayBufferLike | undefined { + getBuffer(key: K): ArrayBufferLike | undefined { const func = this.getFunctionFromCache('getBuffer'); return func(key); } - contains(key: string): boolean { + contains(key: K): boolean { const func = this.getFunctionFromCache('contains'); return func(key); } - delete(key: string): void { + delete(key: K): void { const func = this.getFunctionFromCache('delete'); func(key); @@ -127,12 +128,12 @@ export class MMKV implements MMKVInterface { }; } - addOnValueChangedListener(onValueChanged: (key: string) => void): Listener { - this.onValueChangedListeners.push(onValueChanged); + addOnValueChangedListener(onValueChanged: (key: K) => void): Listener { + this.onValueChangedListeners.push(onValueChanged as ListenerType); return { remove: () => { - const index = this.onValueChangedListeners.indexOf(onValueChanged); + const index = this.onValueChangedListeners.indexOf(onValueChanged as ListenerType); if (index !== -1) { this.onValueChangedListeners.splice(index, 1); } diff --git a/package/src/MemoryWarningListener.ts b/package/src/MemoryWarningListener.ts index 9ac5fbbd..e5910d81 100644 --- a/package/src/MemoryWarningListener.ts +++ b/package/src/MemoryWarningListener.ts @@ -2,7 +2,9 @@ import { AppState } from 'react-native'; import type { NativeEventSubscription } from 'react-native'; import { MMKVInterface } from './Types'; -export function addMemoryWarningListener(mmkv: MMKVInterface): void { +export function addMemoryWarningListener( + mmkv: MMKVInterface +): void { if (global.WeakRef != null && global.FinalizationRegistry != null) { // 1. Weakify MMKV so we can safely use it inside the memoryWarning event listener const weakMmkv = new WeakRef(mmkv); diff --git a/package/src/Types.ts b/package/src/Types.ts index 8f7db3e8..eae25ea7 100644 --- a/package/src/Types.ts +++ b/package/src/Types.ts @@ -81,45 +81,45 @@ export interface Configuration { /** * Represents a single MMKV instance. */ -export interface NativeMMKV { +export interface NativeMMKV { /** * Set a value for the given `key`. * * @throws an Error if the value cannot be set. */ - set: (key: string, value: boolean | string | number | ArrayBuffer) => void; + set: (key: K, value: boolean | string | number | ArrayBuffer) => void; /** * Get the boolean value for the given `key`, or `undefined` if it does not exist. * * @default undefined */ - getBoolean: (key: string) => boolean | undefined; + getBoolean: (key: K) => boolean | undefined; /** * Get the string value for the given `key`, or `undefined` if it does not exist. * * @default undefined */ - getString: (key: string) => string | undefined; + getString: (key: K) => string | undefined; /** * Get the number value for the given `key`, or `undefined` if it does not exist. * * @default undefined */ - getNumber: (key: string) => number | undefined; + getNumber: (key: K) => number | undefined; /** * Get a raw buffer of unsigned 8-bit (0-255) data. * * @default undefined */ - getBuffer: (key: string) => ArrayBufferLike | undefined; + getBuffer: (key: K) => ArrayBufferLike | undefined; /** * Checks whether the given `key` is being stored in this MMKV instance. */ - contains: (key: string) => boolean; + contains: (key: K) => boolean; /** * Delete the given `key`. */ - delete: (key: string) => void; + delete: (key: K) => void; /** * Get all keys. * @@ -139,7 +139,7 @@ export interface NativeMMKV { * * @throws an Error if the instance cannot be recrypted. */ - recrypt: (key: string | undefined) => void; + recrypt: (key: K | undefined) => void; /** * Trims the storage space and clears memory cache. * @@ -165,14 +165,12 @@ export interface Listener { remove: () => void; } -export interface MMKVInterface extends NativeMMKV { +export interface MMKVInterface extends NativeMMKV { /** * Adds a value changed listener. The Listener will be called whenever any value * in this storage instance changes (set or delete). * * To unsubscribe from value changes, call `remove()` on the Listener. */ - addOnValueChangedListener: ( - onValueChanged: (key: string) => void - ) => Listener; + addOnValueChangedListener: (onValueChanged: (key: K) => void) => Listener; }