diff --git a/.changeset/stupid-dancers-vanish.md b/.changeset/stupid-dancers-vanish.md new file mode 100644 index 0000000..e8c8141 --- /dev/null +++ b/.changeset/stupid-dancers-vanish.md @@ -0,0 +1,8 @@ +--- +'storage-box': patch +--- + +fix: Type incompatibility with json/serializable types + +Use `Jsonify` if you need to transform a type to a JSON-compatible type + diff --git a/package.json b/package.json index bb8679b..c51c5f2 100644 --- a/package.json +++ b/package.json @@ -47,10 +47,11 @@ "ci:publish": "changeset publish", "prepublishOnly": "pnpm test && pnpm lint && pnpm format:check && pnpm build" }, - "packageManager": "pnpm@8.15.8", + "packageManager": "pnpm@8.15.9", "dependencies": { "@msgpack/msgpack": "3.0.0-beta2", - "debounce": "^2.1.0" + "debounce": "^2.1.0", + "type-fest": "^4.26.0" }, "devDependencies": { "@changesets/cli": "^2.27.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 15b28df..cbf9f01 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: debounce: specifier: ^2.1.0 version: 2.1.0 + type-fest: + specifier: ^4.26.0 + version: 4.26.0 devDependencies: '@changesets/cli': @@ -2884,6 +2887,11 @@ packages: engines: {node: '>=10'} dev: true + /type-fest@4.26.0: + resolution: {integrity: sha512-OduNjVJsFbifKb57UqZ2EMP1i4u64Xwow3NYXUtBbD4vIwJdQd4+xl8YDou1dlm4DVrtwT/7Ky8z8WyCULVfxw==} + engines: {node: '>=16'} + dev: false + /typescript@5.5.4: resolution: {integrity: sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==} engines: {node: '>=14.17'} diff --git a/src/typings.ts b/src/typings.ts index db12ef8..13c9cbe 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -1,3 +1,7 @@ +import type { JsonValue } from 'type-fest'; + +// --------------------- + export type StorageOperations = KVOperations & HashOperations & ListOperations & { @@ -23,12 +27,15 @@ export interface KVOperations< /** * Hash operations */ -export interface HashOperations { - hget(key: string, field: HashField): Serializable | null; - hset(key: string, field: HashField, value: Serializable): void; - hsetex(key: string, field: HashField, value: Serializable, seconds: number): void; - hdel(key: string, field: HashField): void; - hexists(key: string, field: HashField): boolean; +export interface HashOperations< + Field extends HashField = HashField, + Value extends Serializable = Serializable, +> { + hget(key: string, field: Field): Value | null; + hset(key: string, field: Field, value: Value): void; + hsetex(key: string, field: Field, value: Value, seconds: number): void; + hdel(key: string, field: Field): void; + hexists(key: string, field: Field): boolean; hsize(key: string): number; hclear(key: string): void; hgetall(key: string): HashRecord; @@ -37,7 +44,7 @@ export interface HashOperations { /** * List operations */ -export interface ListOperations { +export interface ListOperations { lset(key: string, index: number, value: Value | null): void; lsetex(key: string, index: number, value: Value, seconds: number): void; lget(key: string, index: number): Value | null; @@ -68,6 +75,9 @@ export interface IStorageParser { parse(value: any): Map; } +export type ParserStringifyFn = (value: any) => string; +export type ParserParseFn = (value: any) => Map; + // --------------------- export type HashField = string | number; @@ -85,51 +95,12 @@ export type HashRecord< // --------------------- -export type ParserStringifyFn = (value: any) => string; -export type ParserParseFn = (value: any) => Map; - -// --------------------- - -export type Class = new (...args: any[]) => T; - -// --------------------- - -/** - Matches a JSON object. - - This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`. - - @category JSON - */ -export type JsonObject = { [Key in string]: JsonValue } & { - [Key in string]?: JsonValue | undefined; -}; - -/** - Matches a JSON array. - - @category JSON - */ -export type JsonArray = JsonValue[] | readonly JsonValue[]; - -/** - Matches any valid JSON primitive value. - - @category JSON - */ -export type JsonPrimitive = string | number | boolean | null; - -/** - Matches any valid JSON value. - - @see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`. - - @category JSON - */ -export type JsonValue = JsonPrimitive | JsonObject | JsonArray; - /** Alias to JSON-compatible values. */ -export type Serializable = JsonPrimitive | JsonArray | JsonValue | JsonObject; +export type Serializable = JsonValue | Serializable[]; /** Alias to a list of JSON-compatible values. */ export type SerializableList = Serializable[]; + +// --------------------- + +export type { JsonPrimitive, JsonArray, JsonValue, JsonObject, Jsonify, Class } from 'type-fest';