Skip to content

Commit

Permalink
fix: type incompatibility with json/serializable types (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi authored Sep 2, 2024
1 parent 7f2a681 commit fa5ee22
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
8 changes: 8 additions & 0 deletions .changeset/stupid-dancers-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'storage-box': patch
---

fix: Type incompatibility with json/serializable types

Use `Jsonify<T>` if you need to transform a type to a JSON-compatible type

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
"ci:publish": "changeset publish",
"prepublishOnly": "pnpm test && pnpm lint && pnpm format:check && pnpm build"
},
"packageManager": "[email protected].8",
"packageManager": "[email protected].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",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 22 additions & 51 deletions src/typings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { JsonValue } from 'type-fest';

// ---------------------

export type StorageOperations = KVOperations &
HashOperations &
ListOperations & {
Expand All @@ -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;
Expand All @@ -37,7 +44,7 @@ export interface HashOperations {
/**
* List operations
*/
export interface ListOperations<Value extends HashValue = HashValue> {
export interface ListOperations<Value extends Serializable = Serializable> {
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;
Expand Down Expand Up @@ -68,6 +75,9 @@ export interface IStorageParser {
parse(value: any): Map<string, Serializable>;
}

export type ParserStringifyFn = (value: any) => string;
export type ParserParseFn = (value: any) => Map<string, Serializable>;

// ---------------------

export type HashField = string | number;
Expand All @@ -85,51 +95,12 @@ export type HashRecord<

// ---------------------

export type ParserStringifyFn = (value: any) => string;
export type ParserParseFn = (value: any) => Map<string, Serializable>;

// ---------------------

export type Class<T> = 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';

0 comments on commit fa5ee22

Please sign in to comment.